Android Wear-Archive Notification (stacking notifications)

Source: Internet
Author: User

Original address: http://developer.android.com/wear/notifications/stacks.html


Objective


When creating notifications on a handheld device, you should often merge similar notifications into a single summary notification. For example, if your app receives information and creates a notification, you shouldn't create multiple notifications on your handheld device. When you receive multiple messages, you should use a single notification and display summary information like "2 new messages".



However, a summary notification is less useful on Android Wear devices because users cannot read each piece of information in detail on the wearable device (they must open your app on a handheld device to see more information). Therefore, on the wearable device, you should archive all notifications to a stack. Stacks that contain multiple notifications are displayed as a card, and the user can expand to view the details of each notification. The new Setgroup () method makes all this possible, and it also allows only one summary notification to be provided on the handheld device at the same time.


For more information on designing the notification stack, please refer to the design principles of Android Wear.


Add each Notification to a group (grouping each piece of information)


To create a stack, you need to call the Setgroup () method for each notification and specify the grouping key. Then call the Notfiy () method to send it to the wearable device.

Final static String group_key_emails = "group_key_emails";//Build the notification and pass this builder to Wearablenotif Ications. Buildernotificationcompat.builder Builder = new Notificationcompat.builder (mcontext)         . Setcontenttitle ("New mail From "+ Sender1")         . Setcontenttext (Subject1).         Setsmallicon (R.drawable.new_mail); Notification notif1 = new Wearablenotifications.builder (Builder)         . Setgroup (group_key_emails)         . Build ();// Issue the Notificationnotificationmanagercompat Notificationmanager =        Notificationmanagercompat.from (this); Notificationmanager.notify (NOTIFICATIONID1, Notif);

Then, when you create other notifications, you specify the same grouping key. Then after you call the Notify () method, the notification will appear in the same notification stack as the previous notification, and instead become a new card:

Builder = new Notificationcompat.builder (mcontext)         . Setcontenttitle ("New mail from" + sender2)         . Setcontenttext (SUBJECT2)         . Setsmallicon (R.drawable.new_mail);//Use the same group as the previous Notificationnotification notif2 = new Wearablenotifications.builder (Builder)         . Setgroup (group_key_emails)         . Build (); Notificationmanager.notify (NOTIFICATIONID2, Notif);

By default, the order in which notifications are displayed is determined by your order of addition, and the most recently added notifications will appear at the top. You can also specify an ordinal in the stack for the notification, as long as you pass the ordinal as the second argument to the Setgroup () method.


Add a Summary Notification (adds a summary notification)


It is important to keep a summary notification available on handheld devices. So, in addition to adding each notification to the same stack, add a summary notification to the stack, but set the sequence number of the digest notification to group_order_summary.



This summary notification will not appear in the notification stack on the wearable device, but will appear as a notification on the handheld device.

Bitmap LargeIcon = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_large_icon); builder = new Notificatio  Ncompat.builder (This). Setsmallicon (R.drawable.ic_small_icon). Setlargeicon (LargeIcon);//Use the same group Key and pass this builder to Inboxstyle notificationwearablenotifications.builder wearablebuilder = new Wearablenotificat ions.  Builder (builder). Setgroup (Group_key_emails, wearablenotifications.group_order_summary);//Build the Final notification to show on the handsetnotification summarynotification = new Notificationcompat.inboxstyle (WEA Rablebuilder.getcompatbuilder ()). AddLine ("Alex faaborg Check this Out"). AddLine ("Jeff Chang Launch Par Ty "). Setbigcontenttitle (" 2 new Messages "). Setsummarytext (" [email protected] "). build (); notification Manager.notify (NOTIFICATIONID3, summarynotification);

This notification uses Notificationcompat.inboxstyle, which provides a simple way to create notifications for mail or information-class applications. You can use this style, while other notifications are defined using Notificationcompat, and of course you can not define summary notifications in this style at all.


Tip: Define similar text styles in the styling with HTML markup and styling with Spannables.

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.