Stacking Notifications(层叠多 个Notification)

来源:互联网 发布:可以拍电影的软件 编辑:程序博客网 时间:2024/06/08 15:39

前面呢,我们说了这个多个页面,这里的话,假如说,同一个app在短时间呢,来了多条通知,但是,用户沒有看,所以这样子的话,就有多条通知了是吧。在手机端呢,多条通知,也会层叠起来,而在手表端是怎么样的呢?看看就知道了哈!

老规矩,要翻墙。

原文地址:https://developer.android.com/training/wearables/notifications/stacks.html

Stacking Notifications(层叠notification)

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."

当我们给这个手表创建通知的时候,你总是把雷同的通知扔到同一个通知里头。就好像,如果你的app创建接收消息的通知,你不应该展示多个Notification在手机设备上吧。当超过一条消息接收到时,使用一个Notificaiton显示接收到的摘要即可,比如说显示"2条新消息”

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.

可是呢,一个只显示摘要的Notification在手表上用处不大,因为用户从这个手机上不可以读到每一条信息的详情(用户必须在手机上打开app才能看到更多的信息)。所以呢,对于手表来说,你应该把这些通知弄成一个组,层叠起来。只显示为一张卡片,用户可以把卡片展开来读取里面每一条通知的详情,这是用setGroup这个方法来实现的,它允许你在手机上只显示一条通知。

11_bundles_A11_bundles_B

Add Each Notification to a Group(把每一条通知添加到一个组里头)


To create a stack, call setGroup() for each notification you want in the stack and specify a group key. Then call notify() to send it to the wearable.

创建一个堆,调用setGroup方法,并且把一个key传进去,然后呢,再调用notify()方法来把这个通知发送到手表上:

final static String GROUP_KEY_EMAILS = "group_key_emails";// Build the notification, setting the group appropriatelyNotification notif = new NotificationCompat.Builder(mContext)         .setContentTitle("New mail from " + sender1)         .setContentText(subject1)         .setSmallIcon(R.drawable.new_mail)         .setGroup(GROUP_KEY_EMAILS)         .build();// Issue the notificationNotificationManagerCompat notificationManager =        NotificationManagerCompat.from(this);notificationManager.notify(notificationId1, notif);

Later on, when you create another notification, specify the same group key. When you call notify(), this notification appears in the same stack as the previous notification, instead of as a new card:

之后呢,当你创建另外一个通知以后,使用同样的key,当你调用notify(),它就会现在同一个堆里,而不是以新的一个通知来显示。

Notification notif2 = new NotificationCompat.Builder(mContext)         .setContentTitle("New mail from " + sender2)         .setContentText(subject2)         .setSmallIcon(R.drawable.new_mail)         .setGroup(GROUP_KEY_EMAILS)         .build();notificationManager.notify(notificationId2, notif2);

By default, notifications appear in the order in which you added them, with the most recent notification visible at the top. You can order notifications in another fashion by calling setSortKey()

默认情况下,Notification的显示是按你添加的顺序的,你最近添加的呢,就会显示在最顶部,你可以通过setSortKey()这个方法来对这些通知进行排休:

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 and call setGroupSummary() on the summary notification.

在手机上显示摘要通知呢还是很有必要的。所以呢,还要添加每一个独立的通知到同一个堆组里,同时添加一个摘要通知并且调用setGropuSummary()方法。

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

这个通知不会在你手表上的通知里头,但是会显示在手机设备上。

notif_summary_framed

 

 

 

 

 

 

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),        R.drawable.ic_large_icon);// Create an InboxStyle notificationNotification summaryNotification = new NotificationCompat.Builder(mContext)        .setContentTitle("2 new messages")        .setSmallIcon(R.drawable.ic_small_icon)        .setLargeIcon(largeIcon)        .setStyle(new NotificationCompat.InboxStyle()                .addLine("Alex Faaborg   Check this out")                .addLine("Jeff Chang   Launch Party")                .setBigContentTitle("2 new messages")                .setSummaryText("johndoe@gmail.com"))        .setGroup(GROUP_KEY_EMAILS)        .setGroupSummary(true)        .build();notificationManager.notify(notificationId3, summaryNotification);

This notification uses NotificationCompat.InboxStyle, which gives you an easy way to create notifications for email or messaging apps. You can use this style, another one defined in NotificationCompat, or no style for the summary notification.

这通知使用NotificationCompat.InBoxStyle,它呢给你一个很方便的方法来给email或都信息类型的应用创建通知。你可以使用这种风格,另一种NotificationCompat里的定义,或者不使用风格。

Tip: To style the text like in the example screenshot, see Styling with HTML markup and Styling with Spannables.

小贴士,要做到像上图中的文字样式,你可以点击Styling with HTML markup and Styling with Spannables.

Summary notifications can also affect notifications on wearables without being displayed on them. When creating a summary notification, you can use the NotificationCompat.WearableExtender class and call setBackground() or addAction() to set a background image or an action that applies to the entire stack on the wearable. For instance, to set the background for an entire stack of notifications:

摘要通知呢,在不显示的情况下,也可以影响到手表上的通知。当创建一个摘要通知时,你可以使用NotificationCompat.WearableExtender类然后调用setBackground()或者addAction()方法来设置背景图片,或者一个在手表上关联整个堆的Action,例如,设置整个通知堆的背景图片:

Bitmap background = BitmapFactory.decodeResource(getResources(),        R.drawable.ic_background);NotificationCompat.WearableExtender wearableExtender =        new NotificationCompat.WearableExtender()        .setBackground(background);// Create an InboxStyle notificationNotification summaryNotificationWithBackground =        new NotificationCompat.Builder(mContext)        .setContentTitle("2 new messages")        ...        .extend(wearableExtender)        .setGroup(GROUP_KEY_EMAILS)        .setGroupSummary(true)        .build();

 

 

 

0 0
原创粉丝点击