b.lyte
b.lyte

Reputation: 6737

Notification PendingIntent Intent extras are overridden by another notification

When creating a new notification with a new PendingIntent, the extras in its intent override any previous notification's PendingIntent Intent extras.

For example, lets say I create Notification1 with PendingIntent1, which has Intent1 and its extras.

When I create Notification2 with PendingIntent2, which has Intent2 with its own different extras, Intent1 will now have the same extras as Intent2. Why is this happening? How do I work around this?

Upvotes: 11

Views: 4605

Answers (2)

Rahul_Pawar
Rahul_Pawar

Reputation: 592

In my case it worked with by using unique notification id each time like below:

mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());

Generate the unique id each by using (int) System.currentTimeMillis() and give this to NotificationManager object.

Upvotes: 5

b.lyte
b.lyte

Reputation: 6737

There are two ways to solve this:

One is to set a different action on the Intent. So in your example, you could set Intent1.setAction("Intent1") and Intent2.setAction("Intent2"). Since the action is different, Android will not override the extras on the intent.

However, there may be a case where you actually need to set a specific action on this intent (i.e. your action corresponds to a specific broadcast receiver). In this case, the best thing to do is set the request code to something different in each PendingIntent:

PendingIntent pendingIntent1 = PendingIntent.getActivity(context,
    (int) System.currentTimeMillis() /* requestCode */, intent1, 
    PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent pendingIntent2 = PendingIntent.getActivity(context,
    (int) System.currentTimeMillis() /* requestCode */, intent2, 
    PendingIntent.FLAG_UPDATE_CURRENT);

By setting a new request code on the PendingIntent, Android will not override the extras on each of their corresponding intents.

Upvotes: 40

Related Questions