如何将Android Nougat的直接回复功能与NotificationListener结合使用? [英] How to utilize Android Nougat's Direct Reply feature with a NotificationListener?

查看:204
本文介绍了如何将Android Nougat的直接回复功能与NotificationListener结合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用正在使用NotificationListener从各种第三方应用(例如WhatsApp)中读取消息.

到目前为止,如果只有一个聊天未读,我可以发送答复,代码在下面.

但是,对于WhatsApp,当未读两个以上的聊天时,由于消息被捆绑在一起,因此getNotification().actions返回空对象.从下面的图片中可以看到,如果扩展了通知,还可以选择发送直接回复,因此我确信可以利用此功能,我也认为像PushBullet这样的应用程序都在使用此方法./p>

我如何访问该通知的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;
        }
    }

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

下面是两个屏幕截图,第一个屏幕截图正常运行,而第二个屏幕截图正常.

解决方案

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

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

为此,您可以参考此链接(MichałTajchert完成了出色的研究工作). (您可能需要使用NotificationCompat.isGroupSummary来解决)

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

.actions方法返回所有 Notification.Action 的数组 sendNativeIntent, cycles through all RemoteInputs of that Action and adds the message to the intent. If more than one chat is unread, getNotification().actions returns null.

Below are two screenshots, the first one where it is working without any problems and the second one where it doesn't.

解决方案

You can consider this as my suggestion. I have done bit research on this and come up with following conclusions.(Also it looks like you have done plenty of research on this so it might be possible that you aware about what I wrote below)

Numerous apps send Wear specific notifications, and many of those contain actions accessible from an Android Wear device. We can grab those Wear notifications on the device, extracting the actions, finding the reply action (if one exists), populating it with our own response and then executing the PendingIntent which sends our response back the original app for it to send on to the recipient.

To do so you can refer this link (A nice workaround by Rob J). You can also refer this link in this context (Great research work done by Michał Tajchert).(You might need to work around with NotificationCompat.isGroupSummary)

This is what I feel(Might be I am totally wrong)

.actions method returns Array of all Notification.Action structures attached to current notification by addAction(int, CharSequence, PendingIntent), Here addAction method is deprecated one so it might not working as intended.

I am not able to test this at my end otherwise I will love to provide a working solution with code.

Hope this will help you. Happy Coding!!!

这篇关于如何将Android Nougat的直接回复功能与NotificationListener结合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆