網站首頁 編程語言 正文
通知監控NotificationListenerService?onNotificationPosted重復回調問題_Android
作者:自動化BUG制造器 ? 更新時間: 2023-03-28 編程語言正文
通過 NotificationListenerService 監聽第三方應用的通知發現,同一條通知,會回調兩次 onNotificationPosted 方法。
// 第一次回調
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
// 第二次回調
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
解決該問題的思路是如何判斷兩次回調的 StatusBarNotification 對象是同一個通知。
經過日志分析,onNotificationPosted 的時間戳相差毫秒級別,且兩次 StatusBarNotification 對象的 postTime 是相同的。
通過記錄上一次 StatusBarNotification 對象,并與第二次的 StatusBarNotification 對象進行比較去重,
StatusBarNotification 對象有個屬性 key,可以作為唯一識別符:
private String key() {
String sbnKey = user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid;
if (overrideGroupKey != null && getNotification().isGroupSummary()) {
sbnKey = sbnKey + "|" + overrideGroupKey;
}
return sbnKey;
}
并配合 postTime 屬性進行去重:
// 過濾同一條通知
if (lastSbn?.key == sbn.key && lastSbn?.postTime == sbn.postTime) {
return
}
當然,如果你知道一些 Intent 中的額外信息,也可以作為過濾條件:
val text = extras.getString(Notification.EXTRA_TEXT)
val title = extras.getString(Notification.EXTRA_TITLE)
// other ...
原文鏈接:https://juejin.cn/post/7194776281307676729
相關推薦
- 2022-12-26 C語言實現十六進制轉換為十進制的方法詳解_C 語言
- 2023-02-09 詳解如何使用Python實現復制粘貼的功能_python
- 2022-05-12 webshell及木馬詳解
- 2022-10-16 QT編寫tcp通信工具(Client篇)_C 語言
- 2022-12-27 go?build失敗報方法undefined的解決過程_Golang
- 2022-09-13 Oracle使用fy_recover_data恢復truncate刪除的數據_oracle
- 2023-06-03 C/C++中#define的妙用分享_C 語言
- 2022-04-10 el-table中使用el-select,選擇后不刷新解決方法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支