Android developer Zhuo Notification (1) code can be used directly, and Android developer notification

Source: Internet
Author: User

Android developer Zhuo Notification (1) code can be used directly, and Android developer notification

Notification is an ideal method for displaying prompt information in Android. It can transmit application information to our Android desktop status bar, using this message transmission method does not affect users' normal use of mobile phones. In addition, Notification can not only transmit text information, but also transmit image information, and even append our control to it, as long as the user does not clear it, the message will be stored in the status bar of the user's desktop.

Notification is usually used to transmit user update reminders and other messages.

Notification needs to be managed by icationicationmanager (Message Notification management class). The creation of Notification requires five steps:

(1) Get the Notification object through getSystemService

(2) create a Notification. Each notification corresponds to a notification object.

(3) create a PendingIntent object

(4) use the setLatestEventInfo method of the notification class to set the details of the Notification object.

(5) Use icationicationmanager's notify y method to display messages

 

In step 2, you must set the notification message displayed on the status bar above the screen, the image resource ID in front of the notification message, and the time when the notification is released, which is generally the current time.

(3) create a PendingIntent object:

Because the notification can be detached from the application, that is, your application has been closed, and the message is still displayed in the status bar. After the application is started again, you can re-control these notifications, such as clearing and replacement. Therefore, the PendingIntent object is maintained by the Android system. Therefore, after the application is closed, the message will not be released.

(4) when using the notify y method, you must specify the unique ID of the Notification object. This ID must be unique relative to a Notificationmanager object. Otherwise, it will overwrite

Notification

Icationicationmanager manager = (NotificationManager)

GetSystemService (icationication_service );

String notice = "you have a short message! ";

Notification notification = newNotification (R. drawable.Ic_launcher, notice, System. currentTimeMillis ());

PendingIntent contentIntent = PendingIntent. getActivity

(GetApplicationContext (), 0, getIntent (), 0 );

Notification.SetLatestEventInfo

(GetApplicationContext (), "the little Porter has become handsome again", "Huazhong software Leading Enterprise", contentIntent );

Notification. defaults = Notification. DEFAULT_SOUND;

Notification. defaults = Notification. DEFAULT_LIGHTS;

Notification. defaults = Notification. DEFAULT_VIBRATE;

Notification. defaults = Notification. DEFAULT_ALL;

Manager. Policy (R. drawable. ic_launcher, notification );

1. to retrieve the sound, you must use the permission android: name = "android. permission. VIBRATE ".

Icationicationmanager. cancel (resId) clears all messages in a data icationicationmanager. cancelAll () object.
After clearing the message, you need to do some aftercare, through notification and deleteIntent. deleteIntent also needs a pendingIntent type variable for calling when clearing the notification object used. This action can be associated with Activity, Service, and Broadcast.

To permanently Save the push notification, add the following code:

• Notification. flags = Notification. FLAG_NO_CLEAR;

 

Related Article

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.