Android 如何利用安卓牛轧糖&x27;s带有NotificationListener的直接回复功能?
我的应用程序正在使用Android 如何利用安卓牛轧糖&x27;s带有NotificationListener的直接回复功能?,android,android-7.0-nougat,notification-listener,remote-input,Android,Android 7.0 Nougat,Notification Listener,Remote Input,我的应用程序正在使用通知侦听器从各种第三方应用程序(例如WhatsApp)中读取消息 到目前为止,我能够发送一个答复,如果只有一个聊天是未读的,代码如下 但是,在使用WhatsApp的情况下,getNotification().actions在两个以上的聊天未读时返回空对象,因为消息被捆绑在一起。正如你在下面的图片中所看到的,如果通知被扩展,那么也可以选择发送直接回复,因此我确信这是可以利用的,而且我认为像PushBullet这样的应用程序也在使用这种方法 我如何访问该通知的RemoteInpu
通知侦听器
从各种第三方应用程序(例如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;
}
}
上述代码的工作原理:一旦收到通知,应用程序将尝试获取操作并检查名称是否在remoteInput的标题中(通常为“Reply to$name”格式),如果找到,则将操作保存到ReplyInputSender类中,当由sendNativeIntent
触发时,循环执行该操作的所有远程输入,并将消息添加到意图中。如果有多个聊天未读,getNotification().actions
返回null
下面是两个屏幕截图,第一个屏幕截图可以正常工作,第二个屏幕截图没有问题
< P>你可以把这当作我的建议。我对此做了一些研究,并得出了以下结论。(另外,看起来你对此做了大量研究,所以你可能知道我在下面写的内容) 许多应用程序发送特定于磨损的通知,其中许多包含可从Android磨损设备访问的操作。我们可以抓取设备上的磨损通知,提取动作,找到回复动作(如果存在),用我们自己的响应填充它,然后执行
pendingent
,将我们的响应发送回原始应用程序,以便发送给收件人
为此,您可以参考(Rob J的一个很好的解决方法)。您也可以在本文中引用(MichałTajchert所做的伟大研究工作)。(您可能需要使用NotificationCompat.isGroupSummary
)
这就是我的感受(可能是我完全错了)
.actions
方法返回所有
附加到当前通知的结构,此处不推荐使用addAction方法
所以它可能无法按预期工作
我不能在我的最后测试这个,否则我会喜欢用代码提供一个工作的解决方案
希望这对你有帮助。快乐编码 您使用的是NotificationListenerService还是自定义NotificationListener界面?请阅读:@PravinDivraniya我使用的是常规NotificationListenerService。好的……如果可能,请您发布与您的问题相关的侦听器服务代码。如果您愿意,我可以,但侦听器基本上只是将状态通知传递给类。感谢您的输入,我对第一个链接的
。isGroupSummary
部分有点困惑-他不处理groupsummary对吗?它用于一次识别应用程序发布的多个通知。在使用.isGroupSummary identify wear通知的第一个链接中,抓取该通知并从中提取操作和远程输入,然后发送答复。@强制希望此答复对您有所帮助。如果您希望在收到答复之前保留挂起的内容。例如,如果它来自手表。Android 8.0限制后台服务,这怎么可能呢?要坚持下去是不可能的。