堆叠的通知

堆叠的通知

当为手机创建通知时,应该讲一些类似的通知合集为一个概要通知.比如应用程序收到消息时创建一个通知,当收到多个消息时不应该在手机显示多个消息.当收到多个消息时用一个摘要的通知进行展示比如”两条消息”.

然而摘要通知在android wear上作用不是特别明显因为使用者不能读取设备上的每条详细的通知,必须打开手机去看更多的消息.所以对于wear设备应该将收到的通知放到一个组里.这个组里的信息做为一个单独的卡片,用户可以展开这个卡片然后分别查看每个通知的详细信息.方法setGroup()可以完成这个操作,同时仍然只需要提供一个摘要通知.
这里写图片描述
这里写图片描述

向栈中添加一个通知

创建一个栈,对于想添加到栈的通知通过调用setGroup()这个方法,并传递一个分组key就可以了,然后调用nofity()将消息发送到wear.

final static String GROUP_KEY_EMAILS = "group_key_emails";

// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(mContext)
         .setContentTitle("New mail from " + sender1)
         .setContentText(subject1)
         .setSmallIcon(R.drawable.new_mail)
         .setGroup(GROUP_KEY_EMAILS)
         .build();

// Issue the notification
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(this);
notificationManager.notify(notificationId1, notif);

接着,当你创建另外的通知时,指定相同的组key.当你调用nofity()时,此通知会显示在与上一个通知相同的堆栈中,而不是以新卡片展示.

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);

默认的,通知按照你添加的顺序进行展示,最新添加的通知显示在最顶部.另外你可以调用setSortKey()这个方法以另外一种方式排序通知.

为通知添加一个摘要

提供一个显示在手机上的摘要是非常重要的.除了将通知添加到一个相同的栈中,还需要调用setGroupSummary()添加摘要通知.

这个摘要通知不在android wear上展示,但是在手机设备上时是显示的.
这里写图片描述

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
        R.drawable.ic_large_icon);

// Create an InboxStyle notification
Notification 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);

这个通知使用NotificationCompat.InboxStyle,这可以使你方便的为email或者消息应用创建通知.你可以使用这个样式,另一个在NotificationCompat中定义,或者不为摘要通知设置样式.

提示:要为上图屏幕截图中的文本设置样式,参考Styling with HTML markup Styling with Spannables

摘要通知也影响穿戴设备的通知,从而不在通知上显示.创建摘要通知时,可以使用NotificationCompat.WearableExtender这个类调用setBackground()或者addAction()去设置背景图片或者应用于可穿戴设备上的整个堆栈的操作.例如:为整个堆栈的通知设置背景.

Bitmap background = BitmapFactory.decodeResource(getResources(),
        R.drawable.ic_background);

NotificationCompat.WearableExtender wearableExtender =
        new NotificationCompat.WearableExtender()
        .setBackground(background);

// Create an InboxStyle notification
Notification summaryNotificationWithBackground =
        new NotificationCompat.Builder(mContext)
        .setContentTitle("2 new messages")
        ...
        .extend(wearableExtender)
        .setGroup(GROUP_KEY_EMAILS)
        .setGroupSummary(true)
        .build();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值