page.title=Stacking Notifications @jd:body

When creating notifications for a handheld device, you should always aggregate similar notifications into a single summary notification. For example, if your app creates notifications for received messages, you should not show more than one notification on a handheld device—when more than one is message is received, use a single notification to provide a summary such as "2 new messages."

However, a summary notification is less useful on an Android wearable because users are not able to read details from each message on the wearable (they must open your app on the handheld to view more information). So for the wearable device, you should group all the notifications together in a stack. The stack of notifications appears as a single card, which users can expand to view the details from each notification separately. The new setGroup() method makes this possible while allowing you to still provide only one summary notification on the handheld device.

For details about designing notification stacks, see the Design Principles of Android Wear.

Add Each Notification to a Group

To create a stack, call setGroup() for each notification you want in the stack, passing the same group key. For example:

final static String GROUP_KEY_EMAILS = "group_key_emails";

NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
         .setContentTitle("New mail from " + sender)
         .setContentText(subject)
         .setSmallIcon(R.drawable.new_mail);

Notification notif = new WearableNotifications.Builder(builder)
         .setGroup(GROUP_KEY_EMAILS)
         .build();

By default, notifications appear in the order in which you added them, with the most recent notification visible at the top. You can define a specific position in the group by passing an order position as the second parameter for setGroup().

Add a Summary Notification

It's important that you still provide a summary notification that appears on handheld devices. So in addition to adding each unique notification to the same stack group, also add a summary notification, but set its order position to be GROUP_ORDER_SUMMARY.

Notification summaryNotification = new WearableNotifications.Builder(builder)
         .setGroup(GROUP_KEY_EMAILS, WearableNotifications.GROUP_ORDER_SUMMARY)
         .build();

This notification will not appear in your stack of notifications on the wearable, but appears as the only notification on the handheld device.