Android N Notification Direct Reply

编程入门 行业动态 更新时间:2024-10-23 15:26:17

Android N <a href=https://www.elefans.com/category/jswz/34/1757174.html style=Notification Direct Reply"/>

Android N Notification Direct Reply

前言

Android N 牛轧糖来了,其中一项界面的重要变化就是通知栏可以直接回复,对于对话型的应用来说,这个功能是非常的方便和实用。本文简单介绍一下如何去实现这一功能。

核心

技术介绍跟党走, 一个中心,三个基本点。

一个中心: RemoteInput

三个基本点:

  • 设置群组; 
  • 设置群组描述; 
  • 更新。

一个中心 RemoteInput

此处代码借鉴android官网 .html 1. 创建一个RemoteInput对象 用于接收用户输入
// 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. 将这个对象放入notification 的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();
注意: 这个输入会由谁来处理?答案是:你需要在replayPendingIntent中指定。如何指定,以下是示例代码:
Intent replyIntent = new Intent(context, QuickReplyActivity.class);PendingIntent replyPendingIntent = PendingIntent.getActivity(context, 0,replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

3. 将action放入notication, 并且发送之。
// 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);

看下界面效果,至于这颜色文字要怎么去设置,后面会介绍一下。

4. 接收用户输入 RemoteInput.getResultsFromIntent 这段代码放在那里? 答案是: 就放在你定义的QuickReplyActivity.java中,具体可以直接放到onCreate去处理, getIntent就可以获取到相应的intent。
// 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;}
这样你就获得了用户的输入。
5. 使用同样的ID 更新一下通知
// 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);

三个基本点

一般的应用绝不仅仅只有一个通知,比如邮件和短信,会有很多的通知过来。这些通知需要统一用一个群组来管理,这样用户可以非常方便地展开。

基本点之一 设置群组 

每个通知都需要设置群组
Notification.Builder noti = new Notification.Builder(context).setWhen(System.currentTimeMillis()).setGroup(GROUP_KEY_APP);
static final String GROUP_KEY_MMS = "group_key_xxx";

基本点之二 设置群组描述

在所有的通知都发送出去之后,或者之前,你需要再发送一个通知,这个通知比较特别,它是对整个通知的一个描述, 重要的是,它带有setGroupSummary();
final Notification.Builder noti = new Notification.Builder(context).setWhen(System.currentTimeMillis()).setGroup(GROUP_KEY_APP).setGroupSummary(true);

如果不发会怎么样? 呵呵,不发你就会发现你的状态栏出现了很多该应用通知的icon,因为缺少唯一的管理者。
看下界面效果:

基本点之三 更新

更新什么?更新你使用RemoteInput 回复的那个通知,这一点是谷歌特别要求的。 不更新会怎么样?  答案是: 你无法cancel这个通知。 至于如何更新,请参照前面的介绍。

建议

Android官网会建议开发者去给不同的通知设置不同的ID, 只要群组是唯一的即可,这样对于ID的生成和管理比较麻烦, 这里推荐使用TAG。 同类的通知使用同一个ID, 但是TAG不同,TAG完全可以根据APP自身为每个ITEM的编号来进行设置(举个例子:com.android.xxapp:item1, om.android.xxapp:item2...)
notificationManager.notify(notificationTag, notificationId, notification);
后续对于notification的cancel和更新,使用TAG都会非常方便。

界面

1. 通知栏的颜色 
        Notification.Builder noti = new Notification.Builder(context).setWhen(System.currentTimeMillis()).setColor(mContext.getResources().getColor(R.color.colorAccent));
注意一下:这个颜色是统一的,一旦设置你会发现输入的部分和标题都是同样的颜色。包括icon,也会透出同样的颜色。
2. 回复的部分显示出来的'Type Message' 这个字符串来自于setLabel(“Type Message”)


申明: 此文章属于个人文章, 供大家讨论学习,请不要未经本人同意随便转载。

更多推荐

Android N Notification Direct Reply

本文发布于:2024-03-15 09:22:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1738574.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Notification   Android   Reply   Direct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!