Android Official Development Document Training Series Course Chinese: Notification of large view notifications for users

Source: Internet
Author: User

Original address: Http://android.xsoftlab.net/training/notify-user/expanded.html#big-view

Notifications are rendered in two styles in the notification bar: Normal view and large view. Large views are displayed only when the notification is expanded. This only appears when the notification is at the top of the notification bar or when the user taps the notification.

The large view starts appearing on Android 4.1 and does not support the old version. This lesson will show you how to use large view notifications.

This is an example of a normal view:

The following is an example of a large view:

The sample programs shown in this lesson provide the same functionality for users in both normal and large views:

    • You can delay reminders or cancel notifications.
    • Display the reminder text to the user in one way.

The normal view provides the above functionality in the form of activity. Keep this in mind when designing notifications: you first provide a variety of features in normal view, because there are many users interacting with notifications.

Set notifications to start activity

The sample application constructs and publishes a notification using the subclass Pingservice of Intentservice.

In the following code snippet, the Intentservice method Onhandleintent () indicates that a new activity will be launched when the user clicks on the notification. The Setcontentintent () method sets the Pendingintent that is published when the user clicks the notification, so the activity can be started.

Intent resultintent =NewIntent ( This, Resultactivity.class); Resultintent.putextra (Commonconstants.extra_message, msg); Resultintent.setflags (         Intent.flag_activity_new_task | Intent.flag_activity_clear_task);//Because Clicking the notification launches a new ("special") activity,//There ' s no need to create a artificial back stack.Pendingintent resultpendingintent = pendingintent.getactivity ( This,0, Resultintent, pendingintent.flag_update_current);//This sets the pending intent that should is fired when the user clicks the//Notification. Clicking the notification launches a new activity.Builder.setcontentintent (resultpendingintent);
Construct a large view notification

The following code shows how to set a button in a large view notification:

//Sets up the Snooze and Dismiss action buttons that would appear in the  //big view of the notification.  Intent dismissintent = new  Intent (this , Pingservice.class);d ismissintent.setaction (Commonconstants.action_dismiss); Pendingintent Pidismiss = Pendingintent.getservice (this , 0 , Dismissintent, 0 ); Intent snoozeintent = new  Intent (this , Pingservice.class); Snoozeintent.setaction (Commonconstants.action_snooze); Pendingintent Pisnooze = Pendingintent.getservice (this , 0 , Snoozeintent, 0 );  

The following fragment shows how to construct a builder object. It sets the style of the large view to "big text" and sets the text of the reminder message. It also uses the Addaction () method to add the snooze button and the dismiss button, which will appear on the large View notification:

//constructs the Builder object.Notificationcompat.builder Builder =NewNotificationcompat.builder ( This). Setsmallicon (r.drawable.ic_stat_notification). Setcontenttitle (GetString (r.string.notification)). Setcontenttext (getString (r.string.ping)). SetDefaults (Notification.default_all)//requires vibrate permission        /* Sets the big view "big text" style and supplies the * text (the user ' s reminder message) that Wil         L be displayed * in the detail area of the expanded notification.         * These calls is ignored by the support library for * pre-4.1 devices. */. SetStyle (NewNotificationcompat.bigtextstyle (). BigText (msg)). Addaction (R.drawable.ic_stat_dismiss, GetString (R.string.dismiss), Pidismiss). Addaction (R.drawable.ic_stat_snooze, getString (R.strin G.snooze), Pisnooze);

Android Official Development Document Training Series Course Chinese: Notification of large view notifications for users

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.