Android N Notification Direct Reply
2016-08-08 14:38
183 查看
前言
Android N 牛轧糖来了,其中一项界面的重要变化就是通知栏可以直接回复,对于对话型的应用来说,这个功能是非常的方便和实用。本文简单介绍一下如何去实现这一功能。核心
技术介绍跟党走, 一个中心,三个基本点。
一个中心: RemoteInput
三个基本点:
设置群组;
设置群组描述;
更新。
一个中心 RemoteInput
此处代码借鉴android官网 https://developer.android.com/preview/features/notification-updates.html1. 创建一个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 6.0 fota升级更新modem的方法
- 关于对Android的videoView和mediaController的吐槽
- android textview自动换行、排列错乱问题及解决
- Android Bundle类
- 安卓系统级返回键和HOME键
- android socket 客户端实例
- [置顶] 浅谈Android中的 Fragment、生命周期回调方法 以及使用
- Android WebView 与js交互初探
- 修改双域应用安装路径
- Android实践之ScrollView中滑动冲突处理
- Android学习笔记039之文件上传和下载
- 从心开始学Android:ListView
- Android 正则表达式
- Android 自定义View UC下拉刷新效果(二)
- 在Activity内和Dialog弹窗两种情况下,实现自动弹出软键盘
- 自定义底部菜单,当弹出输入法时,底部菜单被挤到输入法上面
- Android 自定义View UC下拉刷新效果(一)
- Android5.0录屏方案
- Android5.0录屏方案
- Android RecyclerView和ListView使用对比分析