Android Official Development Document Training Series Chinese version: Notify users of updates or remove notifications

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.