Android N 前瞻 - Notification

发布时间:2019-06-12 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android N 前瞻 - Notification脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

GOOGLE 日前已经发布了 AndROId N 的开发者预览版,这比预期要提前了两个月。虽然国内的 rom 通常会慢拍,但是作为开发者提前了解一下 Android N 中的一些新特性还是很有必要的。

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

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

  • 能够直接在 Notification 中回复消息和更新任务事项了。

  • 现在相关的Notification 能够被收纳进一个通知组,而不是再堆满用户的通知栏了。

新的通知模板

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

Android N 前瞻 - Notification

                           Android N 通知示例.png

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

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

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

直接回复

Android N 允许用户直接在通知中回复消息,用户可以在专注于自己当前任务的同时来回复消息了,这个内置的回复操作是通过按钮的形式存在于通知中的。

Android N 前瞻 - Notification

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

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

Android N 前瞻 - Notification

     用户输入回复(图片来自官网).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 官方文档

脚本宝典总结

以上是脚本宝典为你收集整理的Android N 前瞻 - Notification全部内容,希望文章能够帮你解决Android N 前瞻 - Notification所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。