Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 922
shoke Avatar asked Jul 10 '14 01:07

shoke


People also ask

What is the difference between intent and PendingIntent?

In conclusion, the general and main difference between Intent and PendingIntent is that by using the first, you want to start / launch / execute something NOW, while by using the second entity you want to execute that something in the future.

What is pending intent in Android notification?

In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.

What is pending intent callback?

With a PendingIntent, an app can pass an Intent to a second application that can then execute that Intent as if it were the originating app (i.e., with the same permissions). This allows other apps to call back to the originating app's private components.


2 Answers

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.

like image 175
shoke Avatar answered Nov 04 '22 02:11

shoke


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.

like image 41
Rahul_Pawar Avatar answered Nov 04 '22 02:11

Rahul_Pawar