【发布时间】:2019-09-15 13:51:56
【问题描述】:
我正在开发一个 android 应用程序,其中一部分需要通过通知读取 WhatsApp 消息并对其进行分析。 我正在使用 NotificationListenerService 并且它正在工作,但是,当我收到通知然后从其他聊天中收到消息时,我遇到了通知被“分析”两次的问题。 p>
我想要的是对每条消息(通知)进行一次分析。
我已经尝试将通知 sortKey 或 StatusBarNotification 键保存在 HashSet 中,然后每次检查它是否已经包含该键,但这不起作用。
这是 onNotificationPosted 函数中的代码 -
String pack = sbn.getPackageName();
if (pack.equals("com.whatsapp")) {
Bundle extras = sbn.getNotification().extras;
if (extras.getCharSequence("android.text") != null && extras.getString("android.title") != null) {
if (sbn.getNotification().getSortKey() != null) {
String title = extras.getString("android.title");
String text = extras.getCharSequence("android.text").toString();
//Checking if it's from specic group and analyzing the message and
//doing what needs to be done, not related to the problem.
}
}
}
我想要的结果是,如果其他聊天中有其他消息到达,每个通知都将被分析一次,并且不会再次发布
【问题讨论】:
标签: java android android-studio whatsapp