Notification passes old Intent Extras

AndroidAndroid NotificationsAndroid PendingintentAndroid Notification-Bar

Android Problem Overview


i am creating a notification inside a BroadcastReceiver via this code:

String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
        int icon = R.drawable.ic_stat_notification;
		CharSequence tickerText = "New Notification";
		long when = System.currentTimeMillis();

		Notification notification = new Notification(icon, tickerText, when);
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		long[] vibrate = {0,100,200,200,200,200};
		notification.vibrate = vibrate;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
		
		CharSequence contentTitle = "Title";
		CharSequence contentText = "Text";
		Intent notificationIntent = new Intent(context, NotificationActivity.class);
		notificationIntent.putExtra(Global.INTENT_EXTRA_FOO_ID, foo_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

		int mynotification_id = 1;

		mNotificationManager.notify(mynotification_id, notification);

When I click on the notification, it opens the NotificationActivity and inside the Activity i can retrieve the foo_id from the Intent-Bundle (e.g. 1)

However if another notification is triggered and i click on it again, the activity still receives the "old" value (1) from the Intent-Bundle. I've tried to clear the bundle with clear(), but am receiving the same effect. I think sth is wrong with my code..

Android Solutions


Solution 1 - Android

You are sending the same request code for your pending intens. Change this:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

To:

PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);

intents are not created if you send the same params. They are reused.

Solution 2 - Android

Alternatively, you can use the following code to generate your PendingIntent:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

From the doc for PendingIntent.FLAG_UPDATE_CURRENT:

> If the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

Solution 3 - Android

You are passing the same ID. In this kind of situation, make a unique id from time like this:

int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);

And put it as this:

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),iUniqueId, intentForNotification, 0);

Solution 4 - Android

For anyone looking for the best approach after a long time all, you need to pass the PendingIntent.FLAG_UPDATE_CURRENT as the last argument as shown below

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

you don't even need to provide a new unique id.

You need to do this for next time onwards not for the first time

Solution 5 - Android

Your request code is 0 for all the notification. Change following line:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

With:

PendingIntent contentIntent = PendingIntent.getActivity(context, new Random().nextInt(), notificationIntent, 0);

Solution 6 - Android

Just wanted to add another option

 PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrianMView Question on Stackoverflow
Solution 1 - AndroidIncrediAppView Answer on Stackoverflow
Solution 2 - AndroidChristophKView Answer on Stackoverflow
Solution 3 - AndroidhderangaView Answer on Stackoverflow
Solution 4 - AndroidGentleView Answer on Stackoverflow
Solution 5 - AndroidFaisal ShaikhView Answer on Stackoverflow
Solution 6 - AndroidpellucideView Answer on Stackoverflow