Android Notification,notificationmanager Detailed

Source: Internet
Author: User

we know that when using Android notifications, we will use Notificationmanager, notification These two classes, the roles of the two classes are :

Notificationmanager: is the status bar notification management class, responsible for sending notifications, clear notification and so on.

Notification: status bar Notification object, you can set icon, text, cue sound, vibration and so on parameters.

It needs to be declared here, because Android's system upgrade, Android in the notice this piece also has a lot of old things abandoned, one is api11 version, one is API16 version. Let's compare the usage before api11 this is generic:

Pendingintent pendingintent = pendingintent.getactivity (this, 0,                      new Intent (this, mainactivity.class), 0);              The following need to be compatible with the Android 2.x version is the processing mode               Notification notify1 = new Notification ();              Notify1.icon = R.drawable.message;              Notify1.tickertext = "Tickertext: You have a new short message, please note check!" ";              Notify1.when = System.currenttimemillis ();              Notify1.setlatesteventinfo (This, "Notification Title", "This is the                      Notification message", pendingintent);              Notify1.number = 1;              Notify1.flags |= Notification.flag_auto_cancel;             Manager.notify (Notification_flag, notify1);  
The use of API11-API16 is this (mainly the addition of a custom notification icon, and the configuration of the notification has changed)

Pendingintent PendingIntent2 = pendingintent.getactivity (this, 0,                      new Intent (this, mainactivity.class), 0);              Notification notify2 = new Notification.builder (this) is supported after API11                      . Setsmallicon (r.drawable.message)                     . Setticker ("Tickertext:" + "You have a new short message, please pay attention to check!") ").                     setcontenttitle (" Notification Title ").                      Setcontenttext (" This is the Notification message ")                    . Setcontentintent (PendingIntent2)                    . Setnumber (1)                     . GetNotification ();//need to be aware that build () is at API level              // 16 and after the increase, in the API11 can use Getnotificatin () to replace              notify2.flags |= notification.flag_auto_cancel;              Manager.notify (Notification_flag, notify2);  

After api16

Pendingintent PendingIntent3 = pendingintent.getactivity (this, 0,                      new Intent (this, mainactivity.class), 0);              Notification notify3 = new Notification.builder (this) is supported after API16                      . Setsmallicon (r.drawable.message)                      . Setticker ("Tickertext:" + "You have a new short message, please pay attention to check!") ").                      setcontenttitle (" Notification Title ").                      Setcontenttext (" This is the Notification message ")                      . Setcontentintent (PENDINGINTENT3). Setnumber (1). build ();             Notify3.flags |= Notification.flag_auto_cancel; Flag_auto_cancel indicates that the notification will be cleared when the notification is clicked by the user.              manager.notify (Notification_flag, notify3);//Correlation notification

What we're talking about here is mainly the use of api16.

First we get the Notificationmanager object through the service of the system, and then send the message to the system through him, get the method as follows:

Notificationmanager nm = (Notificationmanager) getsystemservice (Notification_service);  

notification mainly contains the following parameters:

    • An icon (notification icons)
    • A title and expanded message (title and content of the notification)
    • APendingIntent   (点击通知执行页面跳转)
Use process:

1. Create Notificationmanager

Via Notificationmanager nm = (Notificationmanager) getsystemservice (notification_service); get NOTIFICATION Notificationmanager Message Management class,

2, create notification entity

by Notification.builder Builder = new Notification.builder (this), create a notification entity that can contain many parameters, such as the notification icon, message content, jump, etc.

3, through Notificationmanager.notify (0, Builder.build ()); Bind the message, it will use notificationservice (not explained here)

General Notice

Notification.builder Builder = new Notification.builder (this);        Intent mintent = new Intent (Intent.action_view, Uri.parse ("Http://blog.csdn.net/xiangzhihong8"));        Pendingintent pendingintent = pendingintent.getactivity (this, 0, mintent, 0);        Builder.setcontentintent (pendingintent);        Builder.setsmallicon (r.drawable.lanucher);        Builder.setlargeicon (Bitmapfactory.decoderesource (Getresources (), r.drawable.lanucher));        Builder.setautocancel (true);        Builder.setcontenttitle ("General notice");        Builder.setcontenttext ("You have a new short message, please pay attention to check");        Notificationmanager.notify (0, Builder.build ());
Folding Notifications

We can also pass the remoteviews (here is the desktop widget implementation, do not know whether people still have the impression)

Notification.builder Builder = new Notification.builder (this);        Intent mintent = new Intent (Intent.action_view, Uri.parse ("http://blog.csdn.net/itachi85/"));        Pendingintent pendingintent = pendingintent.getactivity (this, 0, mintent, 0);        Builder.setcontentintent (pendingintent);        Builder.setsmallicon (r.drawable.foldleft);        Builder.setlargeicon (Bitmapfactory.decoderesource (Getresources (), r.drawable.lanucher));        Builder.setautocancel (true);        Builder.setcontenttitle ("folding Menu");        Builder.setcontenttext ("You have a new short message, please pay attention to check");        Use Remoteviews to create a custom notification view        remoteviews remoteviews = new Remoteviews (Getpackagename (), R.layout.view_ fold);        Notification Notification = Builder.build ();        Specifies the view when expanded        Notification.bigcontentview = remoteviews;        Notificationmanager.notify (1, notification);

Custom Notifications

Notification.builder Builder = new Notification.builder (this);        Intent mintent = new Intent (Intent.action_view, Uri.parse ("Http://blog.csdn.net/xiangzhihong8"));        Pendingintent pendingintent = pendingintent.getactivity (this, 0, mintent, 0);        Builder.setcontentintent (pendingintent);        Builder.setsmallicon (R.drawable.foldleft);        Builder.setlargeicon (Bitmapfactory.decoderesource (Getresources (), R.drawable.icon));        Builder.setautocancel (TRUE);        Builder.setcontenttitle ("custom Menu");        Builder.setcontenttext ("You have a new short message, please pay attention to check");        Set Click Jump Intent hangintent = new Intent (this,notificationactivity.class);        Hangintent.setflags (Intent.flag_activity_new_task);        Pendingintent hangpendingintent = pendingintent.getactivity (this, 0, hangintent, pendingintent.flag_cancel_current);        Builder.setfullscreenintent (Hangpendingintent, true); Notificationmanager.notify (2, Builder.build ());


Source: http://download.csdn.net/detail/xiangzhihong8/9639345


Android Notification,notificationmanager Detailed

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.