我的应用程序正在使用NotificationListener
读取来自各种 3rd 方应用程序的消息,例如 WhatsApp。
到目前为止,如果只有一个聊天未读,我就能够发送回复,代码如下。
然而,就 WhatsApp 而言,getNotification().actions
当两个以上的聊天未读时,返回 null 对象,因为消息捆绑在一起。正如您在下面的图片中看到的,如果通知延长了,也可以选择发送直接回复,因此我确信可以利用它,而且我认为像 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 的所有 RemoteInput 并将消息添加到意图中。如果有多个聊天记录未读,getNotification().actions
返回空值。
下面是两个屏幕截图,第一个屏幕截图可以正常工作,第二个屏幕截图则没有任何问题。
你可以考虑这个作为我的建议。我对此做了一些研究,并得出了以下结论。(而且看起来你对此做了很多研究,所以你可能知道我在下面写的内容)
许多应用程序发送特定于 Wear 的通知,其中许多包含可通过 Android Wear 设备访问的操作。我们可以抓取设备上的这些 Wear 通知,提取操作,查找回复操作(如果存在),用我们自己的响应填充它,然后执行PendingIntent
它将我们的响应发送回原始应用程序,以便将其发送给收件人。
为此,您可以参考这个链接 http://iamrobj.com/how-to-reply-to-any-messenger-on-android-whatsapp-hangouts-etc-directly-from-your-app/(Rob J 提出的一个很好的解决方法)。您还可以参考这个链接 https://www.polidea.com/blog/How_to_respond_to_any_messaging_notification_on_Android/在这种情况下(Michał Tajchert 所做的伟大研究工作)。(您可能需要解决NotificationCompat.isGroupSummary
)
这就是我的感受(也许我完全错了)
.actions
方法返回全部的数组通知.操作 https://developer.android.com/reference/android/app/Notification.Action.html附加到当前通知的结构添加动作(int,
字符序列、待定意图) https://developer.android.com/reference/android/app/Notification.Builder.html#addAction(int,%20java.lang.CharSequence,%20android.app.PendingIntent), 这里不推荐使用addAction方法
一个,因此它可能无法按预期工作。
我无法在最后对此进行测试,否则我很乐意提供带有代码的工作解决方案。
希望对你有帮助。快乐编码!!!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)