gpt4 book ai didi

Android NotificationListenerService onNotificationPosted 触发两次

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:02:29 25 4
gpt4 key购买 nike

我会收听 WhatsApp 消息等通知。

但每次通知进入 NotificationListenerService 时都会触发两次。

有人知道这个问题吗??

这是 AndroidManifest.xml 的一个 fragment :

<service android:name=".NotifyService"
android:label="WhatsNotify"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"></action>
</intent-filter>
</service>

在 NotificationListenerService 类中:

public class NotifyService extends NotificationListenerService {

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i("NotifyService", "got notification");
}
}

编辑:两个 StatusBarNotification 的属性:

第一个通知:

0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073

第二次通知:

0|com.whatsapp|1|null|10073

最佳答案

我不确定为什么会这样。也许通知标志可能会触发它两次。

您可以尝试自己省略重复执行:

public class NotifyService extends NotificationListenerService {
private String mPreviousNotificationKey;
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
if(TextUtils.isEmpty(mPreviousNotification) || !TextUtils.isEmpty(mPreviousNotification) && !sbn.getKey().equals(mPreviousNotificationKey)){
Log.i("NotifyService", "got notification");
}
}

每个 StatusBarNotification 都有生成的唯一键:

private String key() {
return user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid;

}

持有每个前一个键可以区分给定包的后一个通知。

关于Android NotificationListenerService onNotificationPosted 触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33412124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com