Original address: Http://android.xsoftlab.net/training/notify-user/managing.html#Removing
You should avoid creating new notifications when you need to publish notifications for the same event type at different times. Instead, consider updating the original notification, such as changing certain values of the notification or adding some information to the notification.
The following sections describe how notifications are updated and how to remove them.
Modify Notification
In order to set up notifications that can be updated, you need to specify the ID of the notification by the Notificationmanager.notify (ID, notification) method when you publish the notification. To update this notification, an Notificationcompat.builder object needs to be updated or created, and a notification object is built by this object, and the notification object is published with the same ID.
The following code snippet demonstrates how a notification will be used to update the number of events when an event occurs:
Mnotificationmanager = (Notificationmanager) getsystemservice (Context.notification_service);//sets an ID for the notification, so it can be updatedintNotifyid =1; Mnotifybuilder =NewNotificationcompat.builder ( This). Setcontenttitle ("New Message"). Setcontenttext ("You ' ve received new messages."). Setsmallicon (r.drawable.ic_notify_status) nummessages =0;//Start of a loop that processes data and then notifies the user... mnotifybuilder.setcontenttext (currenttext). Setnumber (++nummessages);//Because The ID remains unchanged, the existing notification is //updated.Mnotificationmanager.notify (Notifyid, Mnotifybuilder.build ());
Remove notifications
The notification will be removed from the notification bar when the following event occurs:
- The user removed the notification or used the "Clear All" feature (if the notification is removable).
- The user clicked the notification, which was created with the Setautocancel (False) method (False is the default property).
- By calling the Cancel () method and specifying the ID of the notification. This method can also remove the in-progress notification.
- Remove all notifications that have been published by calling the Cancelall () method.
Android Official Development Document Training Series Chinese version: Notify users of updates or remove notifications