39

I have an activity that is being opened from the notification bar, but when I do NotificationManager.notify(...), I'm giving to the intent a different bundle, so that each notification opens the same activity, but obtaining from the DB other information each other.

But when I try to enter to any of the notifications (for example there are 3 notifications), they all send me to the activity with the same bundle that the last one. After trying with some Flags, I really don't know where is the problem (some flags makes the notification enter to the activity with the first bundle).

I'm following the way they used it in the tutorial.

tshepang
  • 10,772
  • 21
  • 84
  • 127
Daniel
  • 391
  • 1
  • 3
  • 3
  • Can you share some of the code where you are creating the Pending Intents? I had a similar issue and it was because they were the pending intents were being reused by the framework. – Maximus May 20 '11 at 02:54

1 Answers1

113

If the PendingIntent has the same operation, action, data, categories, components, and flags it will be replaced.

Depending on the situation i usually solve this by providing a unique request code either as static values (0,1,2) or the row id of the data I'm receiving from the DB.

PendingIntent.getActivity(context, MY_UNIQUE_VALUE , notificationIntent, PendingIntent.FLAG_ONE_SHOT);

Then I use the same unique value for notify() as

mNotificationManager.notify(MY_UNIQUE_VALUE, notification);
rochdev
  • 3,785
  • 2
  • 19
  • 18
  • This did it for me. Many thanks. The developer docs indeed only mention a unique id for the notify method, not for the getActivity method. – hcpl Aug 25 '11 at 15:48
  • 8
    I had the same problem and this solutions works perfectly. I think the problem starts with the [documentation](http://developer.android.com/reference/android/app/PendingIntent.html#getActivity%28android.content.Context,%20int,%20android.content.Intent,%20int%29) - that claims the request code is not being used currently, but it seems it is. Thanks for the answer. – jstr069 Jul 07 '11 at 17:28
  • PendingIntent.FLAG_UPDATE_CURRENT is not workng neither. Thanks for the answer. – syloc Aug 06 '16 at 21:50
  • Thanks, that worked for me. But remember that MY_UNIQUE_VALUE must not be larger than 65535 (2^16) because only the first 16 bits of the request code are allowed to be used (2^8 for fragments). A simple solution is to use (MY_UNIQUE_VALUE % 65535) as requestCode. So it won't go beyond 2^16 ;) – rekt0x Nov 04 '16 at 11:05