Basic methods of using notification

Source: Internet
Author: User

When an application wants to send some hints to the user, and the application is not in the foreground, it can be implemented with notification. After a notification is issued, the top notification bar of the phone will display an icon, and the status bar can see the details later.

I. Basic usage of notifications

Notifications can be created in an activity or in a broadcast sink, and can be created in a service.

Steps to issue a notification:

1.1 Get the Notification Manager class instance:

Notificationmanager manager = Getsystemservice (context.notification_service);

1.2 Build Notifications

Using Notification.builder to construct a notification, the sample code is as follows:

NotificationNoti= New Notification.Builder(Mcontext)
.Setcontenttitle("New Mail from" +Sender.tostring ())
          setcontenttext (subject
         . Setsmallicon (r. Drawable. New_mail)
         . Setlargeicon (abitmap)
         .  

1.3 After you create a notification, you also need to set the notification, such as title, content, layout, etc. Setlatesteveninfo () method can be a notification to set a standard layout, the first parameter is the context, the second parameter is the tittle content, the third parameter is the body content, the fourth parameter does not know what to do, pass a null.

1.4 Call Manaager's Notify () method to send the notification out. For example: Notify (1,notification); The first parameter is the ID of the notification, the ID of each notification should be guaranteed to be different, and the second parameter is the notification we built.

Second, open other applications through notification

With Pendingintent implementation, pendingintent is similar to intent and can specify an intent, such as initiating an activity, starting a service, initiating a broadcast, and so on, unlike intent's tendency to execute immediately, The two pendingintent are more inclined to perform an action in a suitable reality.

Pendingintent mainly provides several static methods for obtaining instances of pendingintent, such as getactivity (),

In the build notification, the defined pendingintent is passed into the ntification.builder.

    

Basic methods of using notification

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.