ANDROID Notification Notification Bar development detailed

Source: Internet
Author: User
Tags home screen

Notification is a message that is displayed outside of your app's general interface. When the app sends a message to the system, the message is first displayed as a chart in the notification bar. To view the details of the message, you need to go to the notification drawer (notificationdrawer). The notification bar and notification drawer (Notificationdrawer) are controlled at the system level and can be viewed at any time, without limitation to the app.


Figure 1: Notification of notification bar


Figure 2. Notification in the Notificationdrawer.

The design of Notification

As an important part of the Android UI, notification has its own design guidelines. If you want to learn how to design notification and their interactions, read the notification chapter of the Android Design Guide.

interface elements of the notification

The notification in the notification drawer is displayed in two ways, depending on your Android version and the status of the notification drawer:

Normal view

This style is the standard display mode of notification drawer.

Wide View

When your notification is expanded, it will show a larger view, which is a new feature after android4.1.

Two scenarios are described in detail below.

Normal view

In the Touton view, notification up to 64DP, even if you create a wide-view-style notification, which is displayed as normal size in the case of non-expansion. The following is an ordinary notification.

Figure 3: Notification in normal condition

The Blue indicator box represents the following meaning:

1. Title

2. Large Icons

3. Notification Content

4. Notification data

5. Small Icons

The release time of 6.Notification. You can set an explicit time by calling Setwhen (), which is the default time that the system receives the notification.

Wide View

This wide-view notification appears only when notification is expanded, and a normal notification can be expanded by gesture manipulation (if it can be expanded). This style of notification has only been supported since android4.1. The following shows the inbox style of the notification:

Figure 4. Wide View notification

You should notice that this kind of notification is actually not much different from the ordinary, the only difference is the number 7-detail area. Different wide view notification here the display is different, there are several styles:

Large icon Style

The detail area shows a bitmap of the highest bit 256DP.

Text Style

The detail area displays a paragraph of text

message box style (Inbox style)

Detail area displays several lines of text

As for how to set the wide view style of notification can refer to this article Applyinga Big view style to a notification.

Create notification

First, some UI information and related actions of notification are assigned to Notificationcompat.builder object, and then through NotificationCompat.Builder.build () To obtain the notification object itself, and then call Notificationmanager.notify () to transmit the notification to the system. A notification object needs to contain the following:

Small Icons (Setsmallicon () get)

Title (Setcontenttitle () get)

Detail text (Setcontenttext () get)

In addition, the rest of the content is optional, to be aware of these optional content reference Notificationcompat.builder documentation.

The action and behavior of notification

While this is optional, you should add at least one behavior for your notification: Allow the user to click Notification to enter an activity for more viewing or further action. A notification can provide a variety of actions, and you should also let the user click on a notification to always have a corresponding response action, usually open an activity. You can also add a button that responds to a click event in notification, such as delaying the alarm or immediately replying to a short message.

Inside notification, an action itself is defined in a pendingintent, Pendingintent contains a intent that launches the activity in your app. To connect pendingintent with a gesture, you need to call the appropriate Notificationcompat.builder method. For example, if you want to start activity when you click Notification Text, you need to call Notificationcompat.builder's Setcontentintent () to add pendingintent. Starting an activity is the most common type of notification action response.

Create a simple notification

The following code snippet shows an open activity notification, note here that getting pendingintent is done by creating a Taskstackbuilder object:

Notificationcompat.builder Mbuilder = new Notificationcompat.builder (this). Setsmallicon (R.drawable.notifica Tion_icon). Setcontenttitle ("My notification"). Setcontenttext ("Hello world!"); /creates an explicit intent for a Activity in your appintent resultintent = new Intent (this, resultactivity.class);//Th E Stack Builder object would contain an artificial back stack for the//started activity.//This ensures that navigating BA Ckward from the Activity leads out of//your application to the Home screen. Taskstackbuilder Stackbuilder = Taskstackbuilder.create (this);//Adds the back stack for the Intent (and not the Intent it Self) stackbuilder.addparentstack (resultactivity.class);//Adds The Intent that starts the Activity to the top of the stack Stackbuilder.addnextintent (resultintent); Pendingintent resultpendingintent = stackbuilder.getpendingintent (0, PENDINGINTENT.FLAG_UPDA te_current); Mbuilder.setcontentintent (ResultpendiNgintent); Notificationmanager Mnotificationmanager = (notificationmanager) getsystemservice (context.notification_service);// Mid allows to update the notification later on.mNotificationManager.notify (mid, Mbuilder.build ());

It's that simple, you can send a system notification to your users with the code above.

Set the width view style for notification

To get a wide view style that can be expanded, notification preferred to create a commonNotificationCompat.Builder对象,然后调用Builder.setStyle()。记住这种notification只有4.1版本以上才支持。

下面的例子演示了在上面的普通notification的基础上添加宽视图风格的notification:

Notificationcompat.builder Mbuilder = new Notificationcompat.builder (this)    . Setsmallicon ( R.drawable.notification_icon)    . Setcontenttitle ("Event tracker")    . Setcontenttext ("Events received") Notificationcompat.inboxstyle Inboxstyle =        new Notificationcompat.inboxstyle (); String[] Events = new string[6];//sets a title for the Inbox style big Viewinboxstyle.setbigcontenttitle ("Event tracker D Etails: ");.//Moves events into the big viewfor (int i=0; i < events.length; i++) {    inboxstyle.addline (events[i]);}  Moves The big view style object into the notification Object.mBuilder.setStyle (Inboxstyle) ...//Issue the notification Here.

ANDROID Notification Notification Bar development detailed

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.