Kevin
Kevin

Reputation: 759

Push Notification shows every time on app launch

I've followed a tutorial and implemented GCM push notification on my existing app. I've set Main activity to show notification. User receives a notification, clicks on it and notification display on Main activity. The problem is on app relaunch it shows notification every time.

Main Activity

public void registerGCM() {
    ....
    protected Void doInBackground(Void... params) {
        ServerUtilities.register(context, uname, uemail, regId);
    }
}

Im calling this function on onCreate of Main activity to register app. This process is working fine.

Here's the BoradcaseReceiver code in Main activity outside of onCreate function.

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
        WakeLocker.acquire(getApplicationContext());
        Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
        WakeLocker.release();
    }
};

It should only show notification once when user clicks it (this works fine), but its also showing notification every time use launch the app. How do i prevent this?

Please let me know if im not clear enough. Any help will be highly appreciated.

Thanks!

Upvotes: 0

Views: 1148

Answers (1)

Fatih Santalu
Fatih Santalu

Reputation: 4701

If i understood you correctly your missing this documentation

add this attrs into your manifest between activity tags for your result activity i.e MainActivity (for more information check link above)

android:excludeFromRecents="true"
android:launchMode="singleTask"
android:taskAffinity=""

i can't see your notification receiver class so make sure you are setting resultIntent flags like

resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

note: dont forget to give every notification an unique id

Upvotes: 1

Related Questions