【问题标题】:How to utilize Android Nougat's Direct Reply feature with a NotificationListener?如何通过 NotificationListener 使用 Android Nougat 的直接回复功能?
【发布时间】:2017-03-15 03:34:59
【问题描述】:

我的应用正在使用NotificationListener 来读取来自各种第 3 方应用(例如 WhatsApp)的消息。

到目前为止,如果只有一个聊天未读,我能够发送回复,代码如下。

但是,对于 WhatsApp,getNotification().actions 在两个以上的聊天未读时返回一个空对象,因为这些消息是捆绑在一起的。如下图所示,如果通知被扩展,也可以选择发送直接回复,因此我确信可以使用此方法,而且我认为像 PushBullet 之类的应用程序正在使用此方法。

如何访问该通知的 RemoteInput?

public static ReplyIntentSender sendReply(StatusBarNotification statusBarNotification, String name) {

            Notification.Action actions[] = statusBarNotification.getNotification().actions;

            for (Notification.Action act : actions) {
                if (act != null && act.getRemoteInputs() != null) {
                    if (act.title.toString().contains(name)) {
                        if (act.getRemoteInputs() != null)
                            return new ReplyIntentSender(act);
                    }
                }
            }
            return null;
        }



public static class ReplyIntentSender {
      [...]

    public final Notification.Action action;

    public ReplyIntentSender(Notification.Action extractedAction) {
            action = extractedAction;
     [...]
    }

private boolean sendNativeIntent(Context context, String message) {
            for (android.app.RemoteInput rem : action.getRemoteInputs()) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putCharSequence(rem.getResultKey(), message);
                android.app.RemoteInput.addResultsToIntent(action.getRemoteInputs(), intent, bundle);
                try {
                    action.actionIntent.send(context, 0, intent);
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
            return false;
        }
    }

上面代码如何工作的一些解释:一旦收到通知,应用程序就会尝试获取操作并检查名称是否在远程输入的标题中(通常格式为“回复 $NAME”) ,如果找到,则将 Action 保存到 ReplyIntentSender 类中,该类在由 sendNativeIntent 触发时,循环遍历该 Action 的所有 RemoteInputs 并将消息添加到意图中。如果多个聊天未读,getNotification().actions 返回 null。

下面是两张截图,第一张可以正常工作,第二张没有问题。

【问题讨论】:

  • 您使用的是 NotificationListenerService 还是您的自定义 NotificationListener 接口?
  • @PravinDivraniya 我正在使用常规 NotificationListenerService。
  • 好的...如果可能的话,如果它与您的问题相关,请您发布该侦听器服务的代码。
  • 如果你愿意,我可以,但监听器基本上只是将 StatusbarNotification 传递给类。

标签: android android-7.0-nougat notification-listener remote-input


【解决方案1】:

您可以将此视为我的建议。我对此进行了一些研究并得出以下结论。(而且看起来您对此进行了大量研究,因此您可能知道我在下面写的内容)

许多应用会发送特定于 Wear 的通知,其中许多包含可从 Android Wear 设备访问的操作。我们可以在设备上获取这些 Wear 通知,提取操作,找到回复操作(如果存在),用我们自己的响应填充它,然后执行 PendingIntent 将我们的响应发送回原始应用程序以供其发送给收件人。

为此,您可以参考this link(Rob J 的一个很好的解决方法)。在这种情况下,您也可以参考this link(Michał Tajchert 所做的出色研究工作)。(您可能需要使用NotificationCompat.isGroupSummary

这就是我的感觉(可能我完全错了)

.actions 方法返回所有Notification.Action 的数组 addAction(int, CharSequence, PendingIntent) 附加到当前通知的结构,此处不推荐使用 addAction 方法 一个,所以它可能无法按预期工作。

我最终无法对此进行测试,否则我很乐意提供带有代码的工作解决方案。

希望这会对您有所帮助。快乐编码!!!

【讨论】:

  • 感谢您的意见,我对第一个链接的 .isGroupSummary 部分有点困惑 - 我说他不处理 GroupSummaries 是对的吗?
  • 用于识别app同时发布的多个通知。在使用 .isGroupSummary 的第一个链接中识别磨损通知,抓取它并从中提取操作和远程输入并发送回复。
  • @Force 希望这个答案对你有帮助。
  • 如果想保留 PendingIntent 直到收到回复。例如,如果它来自手表。限制后台服务的 Android 8.0 怎么可能?持久化 PendingIntents 是不可能的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多