Multiple notifications to the same activity

2019-01-08 12:45发布

问题:

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.

回答1:

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