Android N 前瞻 - Notification

Google 日前已经发布了 Android N 的开发者预览版,这比预期要提早了两个月。虽然国内的 Rom 一般会慢半拍,可是做为开发者提早了解一下 Android N 中的一些新特性仍是颇有必要的。html

在 Android N 中,通知迎来了一些不小的更新,开发者有了更多的方法来控制通知的外观和行为。因此如今来看一下 Android N 中的 Notification 有了哪些新特性:android

  • Google 提供了新的默认通知模板(更加的简洁和清爽)。并发

  • 可以直接在 Notification 中回复消息和更新任务事项了。app

  • 如今相关的Notification 可以被收纳进一个通知组,而不是再堆满用户的通知栏了。ui

新的通知模板this

Android N 内置了不少新的通知模板来供咱们使用,新的通知模板更加清晰和简洁。简单的是,在 Android N 中系统会默认直接使用这些新的模板,因此咱们不须要额外的修改咱们已有的代码。目前在 Android N Preview 版本中的通知栏看起来就是这样:spa

clipboard.png

Android N 通知示例.png

我我的感受新的通知模板相比之前更能让用户专一于通知的内容上了,将小小的通知界面分割的更加简洁了。在不久的之后,这就将称为 Android 的默认通知样式了。code

注意咱们可使用 setColor() 方法来给咱们的通知自定义一种颜色,让通知和应用保持风格的一致。orm

使用 setColor() 方法可以改变通知栏中的应用图标和应用名颜色,也包括下面提到的通知内操做的文字颜色。htm

直接回复

Android N 容许用户直接在通知中回复消息,用户能够在专一于本身当前任务的同时来回复消息了,这个内置的回复操做是经过按钮的形式存在于通知中的。

clipboard.png

Android N 通知回复界面(图片来自官网).png

当用户点击按钮时,系统就会将回复内容关联到指定的 Intent 并发送该 Intent 到对应的 App。

添加内嵌回复操做

为通知添加内置回复操做的步骤:

1.建立 RemoteInput.Builder 的实例,注意这里传入的 KEY_TEXT_REPLY 参数,以后咱们从 Intent 中取到用户输入的数据也须要用到它。

// Key for the string that's delivered in the action's intent.
 private static final String KEY_TEXT_REPLY = "key_text_reply";
 String replyLabel = getResources().getString(R.string.reply_label);
 RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)  
                .setLabel(replyLabel)
                .build();

2.使用 addRemoteInput() 方法来将上面建立的 RemoteInput 和通知的 action 关联起来:

// Create the reply action and add the remote input.
Notification.Action action = new Notification.Action.Builder(
            R.drawable.ic_reply_icon, getString(R.string.label), replyPendingIntent)
            .addRemoteInput(remoteInput)
            .build();

3.为目标 Notification 设置 action 并发出 Notification:

// Build the notification and add the action.
Notification newMessageNotification = new Notification.Builder(mContext)
            .setSmallIcon(R.drawable.ic_message)
            .setContentTitle(getString(R.string.title))
            .setContentText(getString(R.string.content))
            .addAction(action))
            .build();

// Issue the notification.
NotificationManager notificationManager = NotificationManager.from(mContext);
notificationManager.notify(notificationId, newMessageNotification);

系统就会在用户查看通知时提示用户输入回复消息了。

clipboard.png

用户输入回复(图片来自官网).png

获得内部动做发送的数据

当用户在通知内部进行了回复动做以后,应用就须要获取到用户输入的回复。

1.调用 getResultsFromIntent() 并将 Notification 发出的 intent 做为参数,就会返回一个包含了回复信息的 Bundle。

Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);

2.使用 result key 来查询该 Bundle,能够单首创建一个方法来完成这个工做:

// Obtain the intent that started this activity by calling
// Activity.getIntent() and pass it into this method to get the associated string.
private CharSequence getMessageText(Intent intent) {   
    Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);   
    if(remoteInput != null) {        
      return remoteInput.getCharSequence(KEY_TEXT_REPLY);    
    }    
    return null; 
 }

3.使用和以前的通知相同的 Notification ID 来建立并触发另一个新的通知。这条通知的做用是用来提示用户消息已经成功回复了。当建立这条新的通知时,要使用经过 onReceive() 方法传入的 context 对象:

// Build a new notification, which informs the user that the system
// handled their interaction with the previous notification.
Notification repliedNotification = 
            new Notification.Builder(context)
                          .setSmallIcon(R.drawable.ic_message) 
                          .setContentText(getString(R.string.replied))                
                          .build();
// Issue the new notification.
NotificationManager notificationManager = NotificationManager.from(context);
notificationManager.notify(notificationId, repliedNotification);

对于交互属性强的 App,在处理收到的消息时包含额外的上下文信息通会常很是有用。好比,聊天类的 App 可能会想用户经过通知栏回复了消息后来更新用户的消息列表。当用户经过 RemoteInput 方法回复了消息,开发者可使用 setRemoteInputHistory() 来更新消息列表。

设置内部回复的样式

理所固然的 Google 也提供了自定义内部回复栏样式的方法,虽然目前只是可以修改颜色 - -。具体就是咱们能够在建立 notification builder 时使用 setColor() 方法来改变回复栏的颜色。

捆绑通知

Android N 提供了一种新的显示多个通知的方式:捆绑通知。在收到通知的时候,若是有多个属于同一应用的通知,那么就会把这些通知绑定为一个群组。可使用 Builder.setGroup() 方法来捆绑。

捆绑以后的通知就具备了一种层次关系,能够避免由于大量的通知挤满用户的通知栏而惹恼用户了。层级结构的最顶层是父级通知,显示通知群组的摘要信息。用户点击后能够展开通知组,显示其中全部的自通知。

必须设置通知组中的一个通知来做为群组的摘要,该通知就会做为整个通知组的顶层展现,不然你的通知就不会被显示为一个通知组。代码也很简单,只须要在建立通知的时候,同时调用
setGroup(String) 和 setGroupSummary(true) 就能够了。

使用捆绑通知的时机

  • 子通知能够操做,而且每一个子通知具备特定的操做。

  • 子通知中包含用户想要查看的更多信息。

好的捆绑通知示例包括:显示消息列表的短信应用,显示电子邮件列表的电子邮件应用。

以上大概就是 Android N 中对 Notification 的更新,也稍微了解了怎么来具体的实现。除了通知,Google 在新版本中还为 Android 平台加入了不少很是棒的特性,想要了解更详细的内容,能够查看 Android N 官方说明。

参考资料

Android N Notification 官方文档

相关文章
相关标签/搜索