Notifications in Android N

原文地址:
http://android-developers.blogspot.com/2016/06/notifications-in-android-n.html

Posted by Ian Lake, Developer Advocate

Android Notification经常是一种不成则败的交互方式。为了提供更好的用户体验,我们对Android N上的Notification进行了视觉上的更新,增强了对自定义视图的支持,并且扩展了它的功能,如直接回复,新的MessagingStyle和bundled notification。


Same notification, new look


最重要和最显著的改变是,Notification的默认外观和触感有了明显的改变。很多分散在Notification的域被折叠到新的标题行(header row),和应用的图标以及名字一起固定Notification。这种改变确保了title,text和icon会被尽可能的分配更大的空间,所以,新的Notification看上去会比以前的大,且易于阅读。

标题行信息的有用性比以往任何时候都为重要。但程序的目标是Android N时,默认情况下时间是隐藏起来的。如果你有一条对时间有严格要求的Notification(如邮件),你可以调用setShowWhen(true)来显示时间。此外,subtext取代了contenInfo和number:Android N中不会再显示number,只有在目标是Android N以前的版本且不显示subtext的情况下才会显示contentInfo。在所有的情况下,确保subtext的内容是相关的且有用的。例如,在用户只要一个账户时,就不要在subtext中加入账户的邮件地址。


Notification action也重新设计了,action现在出现在Notification底部的工具栏中,该工具栏视觉上与Notification分离。


你会发现在新的Notification中icon消失了,相反,在Notification有限的空间中,更多的空间被分配给lable。然而,icon仍然被需要,在老版本的Android和Android Wear等设备中仍然可用。


如果你使用NotificationCompat.Builder创建Notification且标准style可用时,不需要额外的代码,系统默认的显示一个带有新的外观和触感的Notification。


Better Support for Custom Views

如果你打算使用自定义的RemoteViews创建自己的Notification,适应所有新的style将会是一个挑战。随着新的标题行,扩展功能,action和大型icon的布置作为独立的元素从text + title形式的Notification中独立出来,我们推荐新的DecoratedCustomViewStyle和DecoratedMediaCustomViewStyle来提供所有这些元素,使得你可以使用新的setCustomContentView()方法专注于内容部分。


这也保证了将来Notification有新的外观和触感的改变时,你不需要改动代码,就能自动适应和使用新的Notification。


Direct Reply

Notification action可以启动一个Activity或者通过Service或BroadcastReceiver做后台工作。而Direct Reply允许你创建一个action,该action使得你可以通过Notification内联的输入框直接获取用户输入。

Direct Reply使用相同的RemoteInput API,该API原本是为Android Wear引进的,用于标记一个action可以直接接收用户的输入。
RemoteInput本身会包含信息。例如key,用于稍后接收输入。例如hint text,用于在用户输入前的提示。
// Where should direct replies be put in the intent bundle (can be any string)
private static final String KEY_TEXT_REPLY = "key_text_reply";

// Create the RemoteInput specifying this key
String replyLabel = getString(R.string.reply_label);
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
        .setLabel(replyLabel)
        .build();
当你构建了RemoteInput,你可以通过addRemoteInput()方法与action关联起来。你还可以调用setAllowGeneratedReplies(true)使得Android Wear 2.0可以生成Smart Reply,令用户回复更为方便。
// Add to your action, enabling Direct Reply for it
NotificationCompat.Action action =
    new NotificationCompat.Action.Builder(R.drawable.reply, replyLabel, pendingIntent)
        .addRemoteInput(remoteInput)
        .setAllowGeneratedReplies(true)
        .build();
关于传递给action的参数pendingIntent,
  • 在Android M及之前的版本中(这些版本不支持Direct Reply),你得解锁屏幕,启动Activity并在输入框中接收用户回复,所以它应该指向Activity。
  • 在Android N中,它可以指向Service(工作在一个独立的线程)或BroadcastReceiver(运行在UI线程),这样,就算是在锁屏状态,它们也可以在后台处理输入事件。
用户可以在系统设置里面设定开启/关闭锁屏状态下的Direct Reply功能。

在Service或BroadcastReceiver中提取用户输入的回复,可以使用RemoteInput.getResultsFromIntent()方法:
private CharSequence getMessageText(Intent intent) {
    Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
    if (remoteInput != null) {
        return remoteInput.getCharSequence(KEY_TEXT_REPLY);
    }
    return null;
 }
处理输入后,你必须更新Notification。这里应该有个触发点,隐藏Direct Reply并告知用户他们的回复已经收到并被正确处理。

对于很多模板而言,这里应该调用setRemoteInputHistory()方法,把消息加到Notification的底部。其余的消息应该追加到历史记录直到主要内容被更新(例如有其他人回复)

 然而,如果你开发的是信息类应用,并且期望能看到上下文,你应该使用MessagingStyle,并把所有的消息添加进去。

MessagingStyle

我们通过展示一个正在进行的对话并在MessagingStyle中使用Direct Reply,优化了用户体验。

该Style为addMessage()方法添加的多个messages提供内置的格式化。每条信息都支持传入信息本身,时间戳和发送人(这样有利于支持群聊)。
builder.setStyle(new NotificationCompat.MessagingStyle("Me")
    .setConversationTitle("Team lunch")
    .addMessage("Hi", timestampMillis1, null) // Pass in null for user.
    .addMessage("What's up?", timestampMillis2, "Coworker")
    .addMessage("Not much", timestampMillis3, null)
    .addMessage("How about lunch?", timestampMillis4, "Coworker"));
你会注意到该style支持特别地指定消息来自用户并记录用户的名字(这里是“Me”),还可以设定一个可选的对话标题。虽然你也可以使用BigTextStyle手动实现以上功能,但使用MessagingStyle,Android Wear 2.0用户可以不用点开Notification就可以直接回复,你不需要开发一个完整的Wear应用就可以为他们提供这种无缝的用户体验。

Bundled Notifications

当你使用新的视觉设计,Direct Reply,MessagingStyle以及所有以前我们的好的实践去开发一个好的Notification时,最重要的考虑整体的Notification体验,特别是你发出多个Notifications时(例如,每一个正在进行的通话或每一个新的邮件线程)。

Bundled Notification提供上面这两种最好的实现(把多个Notifications折叠成一组/展开分组):
  • 一个单一概括性的Notification(summary notification),在用户处理其他Notifications或用户想要一次性处理所有Notifications可用
  • 展开上面Notification,在用户想要处理该组Notifications中的某一个Notification时可用
如果你为Android Wear开发 Stacking Notifications,这里使用的API是完全一样的。只需要简单的为每个Notification中加入setGroup()就可以把他们捆绑在一起。并不局限为一个分组,所以你可以适当把Notifications分组。例如,对于邮件应用,你可以为每一个账户绑定一个分组。

创建一个summary notification也是很重要的。通过setGroupSummary(true)指定为summary notification,在Android M和更早的版本中,summary notification是唯一的Notification,它应该概括所有的Notifications。这是使用 InboxStyle的最好时机,尽管它不是必须的。在Android N和将来的版本中,一些信息(如subtext,content intent,delete intent)会从summary notification中提取出来,并用于为bundled notifications产生折叠的Notifications,所以你应该在全版本的API中继续使用summary notification。

为了提高Android N设备的用户体验,4个或4个以上的没有归为任一分组的Notifications会被自动绑定为一个分组。

N is for Notifications

在Android上,Notification是一个持续改善的区域。从Android G时代到现在的各种新功能,Notification在Android整体的用户体验中扮演着一个重要的角色。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值