Android Notification (GO)

Source: Internet
Author: User

Last we mentioned after 4.0ICS, Google for Android platform notification This "sword" injected more fresh elements, make it more practical and beautiful. The style of notification has since become enriched to suit the needs of different situations. Like the recently popular Divine Comedy "Jiangnan Style", in various corners of the world there have been a variety of dialects of the version, for different colors, nationalities, beliefs of people to accept and like. This article we come to play the "leaning Sword" of various style--various styles of notice.

A notification is a message that appears in the interface (notification bar) outside of your application. When you tell the system that a notification needs to be published, it first appears as an icon in the status bar area. To learn more about its details, the user can pull down the status bar to expand to the notification bar to view.



Note: Unless otherwise noted, the Builder mentioned in this article is notificationcompat.builder in the V4 support library, which is introduced after 4.0ICS. and Notification.builder introduced in 3.0HoneyComb.

I. Notification of visual elements

Notifications have two visual styles, depending on the status of the version and drawer:

Standard view: A notification that displays the standard view in notification drawer.

Large view: A large view that can be seen when a notification is expanded. The view is also part of the extended notification, which is introduced into the 4.1Jerry Bean.

1. Normal view

A notification that appears in the standard view with a height of 64DP. Even if you create a notification with a large view, it still appears in standard view until it is expanded. Here is an example of a normal view:


"1": Content title, "2": Large Icon, "3": Content text, "4": Content Append, "5": Small Icon, "6": Publish Time, you can set an explicit value using the Setwhen () method.

2. Large view

A large view can only appear if the notification is expanded, and the notification must be at the top of the notification drawer. The user can expand or collapse it with gestures (two-finger horizontal swipe or vertical swipe). Extensible notification This feature is introduced in 4.1Jerry beans. Here is an example of an Inbox style notification:



Careful readers have found that the visual elements of large views are shared with normal views. The difference is "7": Notification Details area. For the display of this area, various large view notifications have different settings:

Big Picture Style: The detail area shows a height of 256dp bitmap.

Big Text style: The detail area displays a large block of text.

Inbox Style: The detail area contains the number of lines of text.

In addition, the following two notification visual elements only support large views, so use the standard view of the "Step Aside" ^-^:

Big Content title: Allows you to add a title to overwrite the content title of the normal view, but it can only be in the view that is now extensible.

Summary text: Allows you to add a line of text at the bottom of the detail area.

II. Creating notification Features

If you want to specify UI information and actions for notifications in the Notificationcompat.builder object, you must first use the NotificationCompat.Builder.build () method to create a notification. The method returns an notification object that, on publication of the notification object, can be passed to the system by calling the Notificationmanager.notify () method.

1. Required notification content

The following three notification visual elements are required:

Setsmallicon (): Set up a small icon

Setcontenttitle (): Set a content title

Setcontenttext (): Set content detail text

2. Optional notification content

In addition to the 1 described in the three are optional ^-^, the details can refer to the API Notificationcompat.builder class.

3. Notifications of actions

Although the action is optional, you should add at least one action to the notification. An action allows the user to navigate directly from the notification to an activity in the application. In the activity you will see one or more events or do further work. A notification can provide multiple actions. You should always be able to define an action that triggers when the user clicks on the notification. Usually this action will help you to open an application activity. After the 4.1Jerry bean, you can also add a button to the notification to perform additional actions (such as a warning or instant response text message, etc.). The premise is that the functions of these buttons are available in the activity.

In a notification, the action is defined by a Pendingintent object that contains the intent to open an activity of an application. Use the gesture with the Pendingintent object, and then call the appropriate method in the Notificationcompat.builder object. For example: When the user clicks on the notification text in drawer, if you want to open an activity, you can add a Pendingintent object by calling the Setcontentintent () method.

The most common scenario is when users click Notifications to start an activity. You can also open an activity when the user destroys an activity. Note, however, that only after 4.1Jerry beans can you start an activity in drawer by clicking a button. For more information, refer to the Notificationcompat.builder Guide.

4. Compatibility processing

Not all notifications can be used for a specific version of the feature. For example, an action button can only be used after a 4.1Jerry bean. Because only an expandable notification will appear from this version. To ensure maximum compatibility, use NOTIFICATIONCOMPAT and its subclasses to create notifications, preferably notificationcompat.builder. Also, when you implement a notification, follow this procedure:

(1). No matter what version the user uses, all users should be provided with notification of all features. To do this, you need to verify that all features are available in one activity. Sometimes you may need to add a new activity. For example: When you want to call the Addaction () method to control the media stop and play, you should first implement this function in an activity.

(2). When the user clicks on the notification, make sure that all users click on it to start an interface. We need to create a Pendingintent object for the activity. Then call the Setcontentintent () method to add the Pendingintent object to the notification.

(3). You can now add expandable features to the notifications you want to use. Remember that when a user clicks on a notification, they are guaranteed to be available in the activity for those features that you want to add to the notification.

Iii. creating a standard style notification

1. Case code Display

Notification Normalnoti = new Notificationcompat.builder (this)            . Setautocancel (TRUE)//set automatic purge            . Setcontentinfo (string.valueof (++normalnum))//Set up additional content            . Setcontenttitle ("5 new Messages")//Set content title            . Setcontenttext ("[email protected]")//Set content text            . SetDefaults (Notification.default_all)  //Set use all default values (sound, vibration, splash screen, etc.)            . Setlargeicon (icon)//Set Large Icons            . Setsmallicon (R.drawable.stat_notify_gmail)  //Set Small icons            . Setticker ("Normal Notification")//Set status bar prompt information            . build ();

2. Case Effect Display

Iv. Creating Inbox Style Notifications

1. Case code Display

Notification Inboxnoti = new Notificationcompat.builder (this)            . Setautocancel (True)            . Setcontentinfo ( String.valueof (++inboxnum))            . Setcontenttitle ("5 new Mails").            setcontenttext ("[email protected]")            . SetDefaults (Notification.default_all)            . Setlargeicon (Icon)            . Setsmallicon (R.drawable.stat_notify_gmail)            . Setticker ("Inbox Notification")            . SetStyle (New Notificationcompat.inboxstyle ()//Set notification style to Inbox style                . AddLine ("M.lynn reminder"). AddLine ("M.lynn launch"). AddLine ("                M.lynn Hello").                setsummarytext ("+3 More "))//Set Add a line of text at the bottom of the detail area            . Build ();

2. Case Effect Display

v. Create large text style notifications1. Case code Display
Notification Bigtextnoti = new Notificationcompat.builder (this)            . Setautocancel (True)            . Setcontentinfo ( String.valueof (++bigtextnum)).            setcontenttitle ("New Mail").            setcontenttext ("[email protected]")            . SetDefaults (Notification.default_all)            . Setlargeicon (Icon)            . Setsmallicon (R.drawable.stat_notify_gmail)            . Setticker ("BigText Notification")            . SetStyle (New Notificationcompat.bigtextstyle ()//Set notification style to large text style                . BigText ("Helper class for generating Large-format notifications that include a lot of text. This class is a \ "Rebuilder\": the It attaches to a Builder object and modifies its behavior, like so. ")            . Build ();
2. Case Effect Display VI. Create large picture Style notifications1. Case code Display
Notification Bigpicturenoti = new Notificationcompat.builder (this)            . Setautocancel (True)            . Setcontentinfo ( String.valueof (++bigpicturenum))            . Setcontenttitle ("New photo")            . Setcontenttext ("[email protected]")            . SetDefaults (Notification.default_all)            . Setlargeicon (Icon)            . Setsmallicon (R.drawable.stat_notify_gmail)            . Setticker ("BigPicture Notification")            . SetStyle (New Notificationcompat.bigpicturestyle ()//Set notification style for large picture styles                    . BigPicture (Bitmapfactory.decoderesource (Getresources (), r.drawable.fantasy))            . Build ();
2. Case Effect Display

Http://www.cnblogs.com/dyllove98/archive/2013/06/08/3127580.html

Android Notification (GO)

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.