Android Notification Update for same notification

2019-06-13 18:42发布

问题:

I am using the same notification id to show the notification of my app. I am passing an object with the intent to launch the activity. The issue is for same notification id, android caches the initial object which is passed along the intent.

So how can I make sure that the latest object is added in the intent.

Or how can I check if a notification with the id is not cleared in the notification panel and if it's there then I can delete the notification and create a new one.

Note :- I want to avoid the scenario to use new notification id as that keeps creating new notification without removing the older ones.

回答1:

Use PendingIntent.FLAG_UPDATE_CURRENT in your Pending Intent like below

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, NotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);



回答2:

call notificationManager.cancel(NOTIFICATION_ID); before calling notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());