LoginSignup
2
1

More than 5 years have passed since last update.

【Android】NotificationListenerServiceのonNotificationPosted()で2回同じものが来るときの対処法

Posted at

NotificationListenerServiceのonNotificationPosted()で2回同じものが来るときの対処法メモです。

環境

  • Android: 8.1.0
  • kotlin: 1.2.10

サンプルコード

class NLService : NotificationListenerService() {
    private var mPreviousKey = ""

    override fun onNotificationPosted(sbn: StatusBarNotification) {
        val key = "${sbn.id} ${sbn.packageName}"
        if (mPreviousKey == key) return
        mPreviousKey = key

        // do something
    }
}

補足

StatusBarNotificationのidpackageNameが前回来たものと一致してたらreturnしてます。
tagも一致してるか確認してもいいかもしれません。

参考

Android NotificationListenerService onNotificationPosted fire twice - Stack Overflow
https://stackoverflow.com/questions/33412124/android-notificationlistenerservice-onnotificationposted-fire-twice

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1