Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 7 新特性实战:通知直接回复(Direct Reply)

編輯:關於 Android 程式設計

这是「是时候了解 Android 7」系列的第三篇。前两篇聊了多窗口模式和 Shortcut,这次说个轻量但很实用的功能——通知直接回复。

它解决什么问题

以前收到一条短信,想回一句「好」,得点开通知、进应用、找会话、打字、发送,五六步操作。Android 7 之后,通知栏里直接就能输入并发送。iOS 和部分国产 ROM 早有类似能力,但这是原生 Android 第一次原生支持。对用户是省事,对开发者则是一个新的交互入口。

Demo 流程

这个 demo 全程模拟短信场景:点击「发通知」弹出一条带回复按钮的通知;用户输入内容并点发送后,启动一个 Service 模拟发送过程;Service 先延迟一段时间,再推送一条「发送成功」的通知,最后把通知取消。

发送一条支持直接回复的通知

核心就五步:

1. 创建 RemoteInput
2. 创建 PendingIntent,定义点击「发送」时做什么
3. 创建直接回复的 Action
4. 构建 Notification
5. 发出通知

其中第 2、4、5 步都是我们熟悉的写法,真正的新东西是 RemoteInput 和 Action。

第一步:创建 RemoteInput

RemoteInput remoteInput = new RemoteInput.Builder(RESULT_KEY)
        .setLabel("回复这条消息")
        .build();

Builder 需要一个 String 类型的 key,它的用途是在 Service 里取回用户输入内容时作为索引。setLabel 设置的就是输入框的 Hint 文案——本质上,这个输入框就是一个 EditText。

第二步:创建 PendingIntent

Intent intent = new Intent(this, SendMsgService.class);
PendingIntent pi = PendingIntent.getService(this, 1, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);

这里让它指向一个 Service,用来承接发送逻辑。

第三步:创建 Action

NotificationCompat.Action act = new NotificationCompat.Action.Builder(
        R.mipmap.ic_launcher, "回复", pi)
        .addRemoteInput(remoteInput)
        .build();

三个参数分别是图标、按钮文案(对应界面上那个「回复」按钮)和点击后的行为,随后用 addRemoteInput 把上一步的 RemoteInput 挂进来。

第四步:构建通知

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("请问是否需要信用卡?")
        .setContentText("您好,我是XX银行的XX经理,请问您需要办理信用卡吗?")
        .setColor(Color.CYAN)
        .addAction(act)
        .setCategory(Notification.CATEGORY_MESSAGE);

setColor 指定的是强调色,也就是「回复」按钮以及展开后输入框的背景色;addAction 挂载上一步的 Action;setCategory 声明通知类别,这里按消息类处理。

第五步:发出通知

NotificationManager nm = getSystemService(NotificationManager.class);
nm.notify(NOTIFICATION_ID, builder.build());

在 Service 里处理用户的回复

通知发出来只是一半,用户输入的内容还得接住。这套逻辑放在 SendMsgService 里。

取内容用的是 RemoteInput 的静态方法:

Bundle replyBundle = RemoteInput.getResultsFromIntent(intent);
if (replyBundle != null) {
    String reply = replyBundle.getString(MainActivity.RESULT_KEY);
    reply(reply);
}

拿到 Bundle 后,用创建 RemoteInput 时的那个 key 取值即可。

接着模拟发送延迟:

private void reply(final String reply) {
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.d("reply", "reply: " + reply);
            onReply();
        }
    }, 1000);
}

最后更新通知为「回复成功」,两秒后取消:

private void onReply() {
    final NotificationManager nm = getSystemService(NotificationManager.class);
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            Notification notification = new NotificationCompat.Builder(SendMsgService.this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText("回复成功")
                    .build();
            nm.notify(MainActivity.NOTIFICATION_ID, notification);
        }
    });
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            nm.cancel(MainActivity.NOTIFICATION_ID);
        }
    }, 2000);
}

整段代码量不大,真正要记住的是两点:怎么构建带直接回复的通知,以及怎么把用户输入取回来。如果你的应用有 IM、评论、审批这类场景,适配 Android 7 时把这个能力加上,体验提升相当明显。

顺带一提:悬浮式通知

从 Android 5 开始,系统还提供了悬浮式通知(Heads-up Notification),构建时调用 setFullScreenIntent(PendingIntent pi, boolean highPriority) 就能实现。Android 7 的原生短信应用正是把悬浮通知和直接回复组合起来用。

不过自己实现时会碰到一些细节问题:怎么控制悬浮通知何时消失、消失后如何补发一条普通通知。我的做法是用 Handler 延迟发一条普通通知,但仍有瑕疵——用户还在悬浮通知上直接回复时,通知可能已经被取消了。这块目前还没找到合适的原生实现参考,如果你知道 Google 原生短信是怎么处理的,欢迎交流。

Demo 源码:https://github.com/qibin0506/N_notification_quick_reply

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved