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..
For anyone looking for the best approach after long time all you need to pass the PendingIntent.FLAG_UPDATE_CURRENT as the last argument as shown below
you don't even need to provide new unique id.
You are sending the same request code for your pending intens. Change this:
To:
intents are not created if you send the same params. They are reused.
Your request code is 0 for all the notification. Change following line:
With:
You are passing the same ID. In this kind of situation, make a unique id from time like this:
And put it as this:
Alternatively, you can use the following code to generate your PendingIntent:
From the doc for
PendingIntent.FLAG_UPDATE_CURRENT
: