AlexAndro
AlexAndro

Reputation: 1928

Launch activity from multiple notifications overrides the previous intent

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:

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.

.

Upvotes: 5

Views: 1264

Answers (1)

bwegs
bwegs

Reputation: 3767

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);

Upvotes: 5

Related Questions