Notifications in Android

Source: Internet
Author: User

Notification is a common notification method in Android. When you have unread text messages or missed calls, a prompt icon is displayed in the status bar. You can pull down the status bar to read notifications. When used (running in the background), if there is a new message, a sound prompt will be issued, and the status bar will also have a corresponding prompt.

Steps for implementing notification in Android:

1. Obtain the icationicationmanager object
Icationicationmanager has three common methods:
① Cancel (int id) cancels a previously displayed notification. If it is a short notification, it tries to hide it. If it is a persistent notification, it will be removed from the status bar.
② Cancelall () cancels all previously displayed notifications.
③ Y (int id, Notification) permanently sends the notification to the status bar.

2. initialize the notification object
Notification attributes:
Audiostreamtype the type of the audio stream used when the sound starts
When a notification entry is clicked, contentintent executes the configured intent.
Contentview: When a notification is displayed on the status bar, the configured view is also displayed.
Defaults specifies the value to be set to the default value.
Deleteintent when you click the "Clear all notifications" button to delete all notifications, the configured intent is executed.
Image used by the icon status bar
Iconlevel if there are several levels of Status Bar images, set it here
Ledargb LED light color
Flash time when ledoffms LED is disabled (in milliseconds)
Flash time when ledonms led starts (in milliseconds)
Number indicates the number of the event.
Sound notification sound
Information displayed when the tickertext notification is displayed in the status bar
Vibrate Vibration Mode
Timestamp of the when notification

Note: If the notification is resident in the status bar, you can set the flags attribute of notification to flag_ongoing_event.

n.flags = Notification.FLAG_ONGOING_EVENT

3. Set notification display parameters
Use pendingintent to wrap the notification intent, and use setlatesteventinfo of notification to set the notification title, notification content, and other information.

4. send notifications
Use icationicationmanager's notify (int id, Notification) method to send notifications.

The following code demonstrates the use of notification. Download the complete code: android_icationication.rar

Notification implementation steps:

/*** Mainactivity ** @ author zuolongsnil */public class mainactivity extends activity {private button extends ybtn; private button cancelbtn; private icationicationmanager nm; private notification n; // notification ID public static final int id = 1; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); optional ybtn = (button) findviewbyid (R. id. optional Y); cancelbtn = (button) findviewbyid (R. id. cancel); notifybtn. setonclicklistener (New myonclicklistener (); cancelbtn. setonclicklistener (New myonclicklistener (); // 1. get icationicationmanager object Nm = (notificationmanager) getsystemservice (notification_service); // 2. initialize the notification object n = new notification (); // set the notification icon n. icon = R. drawable. y; // set the scroll information of the notification displayed on the status bar. tickertext = "One notification"; // set the notification time n. when = system. currenttimemillis ();} class myonclicklistener implements onclicklistener {@ override public void onclick (view v) {Switch (v. GETID () {case R. id. y: // 3. set intent = new intent (mainactivity. this, icationicationview. class); pendingintent Pi = pendingintent. getactivity (mainactivity. this, 0, intent, 0); N. setlatesteventinfo (mainactivity. this, "Notification title", "Notification content", Pi); // 4. send notification nm. running y (ID, n); break; case R. id. cancel: // cancel notification nm. cancel (ID); break ;}}}}

Activity to jump to after opening the notification:

/*** The activity that jumps after the notification is opened * @ author zuolongsnil */public class icationicationview extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. view); // cancel notification icationicationmanager Nm = (icationicationmanager) getsystemservice (icationication_service); nm. cancel (mainactivity. ID );}}

:

 

Supplement:

Notification prompt method:
① Notification text prompts displayed in the status bar, such:
Notification. tickertext = "Hello notification ";

② Send a prompt, for example:
Notification. defaults | = notification. default_sound;
Notification. Sound = URI. parse ("file: // sdcard/notification/ringer.pdf ");

Notification. Sound = URI. withappendedpath (audio. Media. internal_content_uri, "6 ");

③ Mobile phone vibration, such:
Notification. defaults | = notification. default_vibrate;

// After a delay of 100 milliseconds, the system vibrate for 200 milliseconds. After a pause of 100 milliseconds, the system then shakes for 300 milliseconds.
Long [] vibrate = {100,200,100,300 };
Notification. vibrate = vibrate;

Note: You need to add the permission <uses-Permission Android: Name = "android. Permission. Vibrate"/>

④ LED light flashes, such:
Notification. defaults | = notification. default_lights;
Notification. ledargb = 0xff00ff00;
Notification. ledonms = 300;
Notification. ledoffms = 1000;
Notification. Flags | = notification. flag_show_lights;

 

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.