5
public static void showNotification(Context ctx, int value1, String title, String message, int value2){

    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(ctx, ActivityMain.class);
    int not_id = Utils.randInt(1111, 9999);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationIntent.putExtra("key_1", value1);
    notificationIntent.putExtra("key_2", value2);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx.getApplicationContext());
    stackBuilder.addNextIntent(notificationIntent);
    PendingIntent notifPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    notificationManager.notify(not_id, createNotification(ctx, notifPendingIntent, title, message));
}

This is my code to send and display a notification. I send this from a service. As you see there are 2 extra values that I send with the intent (key_1 and key_2); When I click a notification I should open the activity and see the value1 and value2 of the keys.

The problem is: if another notification arrives before I open any of them, value1 and value2 are overridden. For instance:

  • first notification sends foo and bar

    • second notification sends faz and baz

    • third notification send fubar and fobaz

So I will have 3 notifications in the bar. Now, whatever notification I click, first, second, third, I will see the values sent by the last one fubar and fobaz. What I want is when I click on a specific notification, the activity display the values sent by that notification.

Any help will be highly appreciated. Thanks.

.

AlexAndro
  • 1,918
  • 2
  • 27
  • 53

1 Answers1

5

This is because you're using FLAG_UPDATE_CURRENT:

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.

This is what is replacing your extra data with the data from the last intent.

Instead pass 0 for PendingIntent's default behavior here:

PendingIntent notifPendingIntent = stackBuilder.getPendingIntent(0, 0);
bwegs
  • 3,769
  • 2
  • 30
  • 33
  • Yes, silly me, I have only used `FLAG_UPDATE_CURRENT` and `FLAG_CANCEL_CURRENT` – AlexAndro Mar 25 '15 at 14:20
  • Ok, I have tried your solution but now only the first extra is kept ignoring the other ones! I didn't noticed that first. Any other idea? so I can accept the answer.... – AlexAndro Mar 25 '15 at 14:27
  • @AlexAndro I think the root of your problem lies in the fact that you need to vary the contents of your intents so that `.filterEquals()` doesn't filter out one another because they are so similar. See these answers - http://stackoverflow.com/a/3009690/745750 and http://stackoverflow.com/a/17200687/745750. Just changing the values of your `extras` is not enough to trick the `.filterEquals()` method. – bwegs Mar 25 '15 at 14:40
  • I found that the secret is to use a DIFFERENT `PendingIntent`. I'm gonna give you the credit anyway, for good url you provided, even the hint was into another answer from that question and also here http://stackoverflow.com/questions/15297710/create-new-pending-intent-every-time-in-android – AlexAndro Mar 25 '15 at 14:52