通知監(jiān)控NotificationListenerService onNotificationPosted重復(fù)回調(diào)問題
正文
通過 NotificationListenerService 監(jiān)聽第三方應(yīng)用的通知發(fā)現(xiàn),同一條通知,會(huì)回調(diào)兩次 onNotificationPosted 方法。
// 第一次回調(diào)
2023-01-31 11:42:31.082330 2483 2483 I NotificationMonitorService: onNotificationPosted:StatusBarNotification(pkg=com.tencent.wemeet.app user=UserHandle{0} id=11499522 tag=null key=0|com.tencent.wemeet.app|11499522|null|10076: Notification(channel=wemeet shortcut=null contentView=null vibrate=default sound=android.resource://com.tencent.wemeet.app/2131623938 tick defaults=0x6 flags=0x11 color=0x00000000 vis=PRIVATE)) - 1
2023-01-31 11:42:31.086442 2483 2483 I NotificationMonitorReceiver: notify time: 1675136670845, pending size: 1
// 第二次回調(diào)
2023-01-31 11:42:31.088771 2483 2483 I NotificationMonitorService: onNotificationPosted:StatusBarNotification(pkg=com.tencent.wemeet.app user=UserHandle{0} id=11499522 tag=null key=0|com.tencent.wemeet.app|11499522|null|10076: Notification(channel=wemeet shortcut=null contentView=null vibrate=default sound=android.resource://com.tencent.wemeet.app/2131623938 tick defaults=0x6 flags=0x11 color=0x00000000 vis=PRIVATE)) - 1
2023-01-31 11:42:31.090506 2483 2483 I NotificationMonitorReceiver: notify time: 1675136670845, pending size: 2
解決該問題的思路是如何判斷兩次回調(diào)的 StatusBarNotification 對(duì)象是同一個(gè)通知。
經(jīng)過日志分析,onNotificationPosted 的時(shí)間戳相差毫秒級(jí)別,且兩次 StatusBarNotification 對(duì)象的 postTime 是相同的。
通過記錄上一次 StatusBarNotification 對(duì)象,并與第二次的 StatusBarNotification 對(duì)象進(jìn)行比較去重,
StatusBarNotification 對(duì)象有個(gè)屬性 key,可以作為唯一識(shí)別符:
private String key() {
String sbnKey = user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid;
if (overrideGroupKey != null && getNotification().isGroupSummary()) {
sbnKey = sbnKey + "|" + overrideGroupKey;
}
return sbnKey;
}
并配合 postTime 屬性進(jìn)行去重:
// 過濾同一條通知
if (lastSbn?.key == sbn.key && lastSbn?.postTime == sbn.postTime) {
return
}
當(dāng)然,如果你知道一些 Intent 中的額外信息,也可以作為過濾條件:
val text = extras.getString(Notification.EXTRA_TEXT) val title = extras.getString(Notification.EXTRA_TITLE) // other ...
以上就是通知監(jiān)控NotificationListenerService onNotificationPosted重復(fù)回調(diào)問題的詳細(xì)內(nèi)容,更多關(guān)于NotificationListenerService onNotificationPosted的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android采用GET方法進(jìn)行網(wǎng)絡(luò)傳值
這篇文章主要為大家詳細(xì)介紹了Android采用GET方法進(jìn)行網(wǎng)絡(luò)傳值的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android實(shí)現(xiàn)從底部彈出的Dialog的實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)從底部彈出的Dialog的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值 ,需要的朋友可以參考下2018-04-04
Android 回調(diào)詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 回調(diào)詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android中TextView文本高亮和點(diǎn)擊行為的封裝方法
這篇文章主要介紹了Android中TextView文本高亮和點(diǎn)擊行為的封裝方法,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03
Android垃圾回收機(jī)制解決內(nèi)存泄露問題
這篇文章主要介紹了Android垃圾回收機(jī)制解決內(nèi)存泄露問題的相關(guān)資料,需要的朋友可以參考下2016-02-02
詳解Android Studio無法檢測(cè)新版本問題解決
這篇文章主要介紹了詳解Android Studio無法檢測(cè)新版本問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08

