Android NotificationListenerService onNotificationPosted fire twice

后端 未结 5 949
情深已故
情深已故 2021-01-18 12:35

I listen for notifications like WhatsApp Messages.

But every time a notification comes in the NotificationListenerService fire twice.

Does anyone know this p

5条回答
  •  孤城傲影
    2021-01-18 13:28

    Facing Same Problem For Whatsapp Notification

    I just solve this by generating new key with statusBarNotification.key + statusBarNotification.title

    Now store this key in my SQLiteDatabase

    code written in Kotlin

     override fun onNotificationPosted(sbn: StatusBarNotification?) {
            if(sbn?.tag!=null) 
            {
            var key = sbn?.key ?: null
            var id = sbn?.id
            var postTime = sbn?.postTime
            var packageName = sbn?.packageName ?: null
            var tikerText = sbn?.notification?.tickerText ?: null
    
            var extraBundle: Bundle? = sbn?.notification?.extras ?: null
            var notificationTitle = extraBundle?.get(Notification.EXTRA_TITLE)
            var text = extraBundle?.getCharSequence(Notification.EXTRA_TEXT).toString()
    
            var modifiyedUniq = key + notificationTitle
    
            //check key present in database or not
            if (!databaseHandler.checkNotification(modifiyedUniq!!)) 
             {
                Log.e(TAG, "Notification Key :: ${key}")
                Log.e(TAG, "Notification Id :: ${id}")
                Log.e(TAG, "Notification postTime :: ${postTime}")
                Log.e(TAG, "Notification From :: ${packageName}")
                Log.e(TAG, "Notification TikerText :: ${tikerText}")
                Log.e(TAG, "Notification Title :: ${notificationTitle}")
                Log.e(TAG, "Notification Text :: ${text}")
                //now add this record in database
                databaseHandler.addNotification(notificationData)
             }
            }
     }
    

    this method databaseHandler.checkNotification(modifiyedUniq!!) returns true if record present with this key else it returns false

    each and every time checking the key if no record present means its new notification

    fun checkNotification(key: String): Boolean {
        var isPresent: Boolean = false
        val db = readableDatabase
        val selectALLQuery = "SELECT * FROM $TABLE_NAME WHERE $KEY='${key}'"
        val cursor = db.rawQuery(selectALLQuery, null)
        if (cursor != null) {
            if (cursor.count > 0) {
                cursor.close()
                db.close()
                Log.e("","====================================RECORD PRESEBNT=======================")
                return true
            }
        }
        cursor.close()
        db.close()
        Log.e("","===*******=======********=====RECORD NOT PRESENT===*******=======********=====")
        return isPresent
    }
    

    Notification 0|com.whatsapp|1|XX2X606878@s.whatsapp.net|10171

    tag = 91XX06X78@s.whatsapp.net

    Notification Id :: 1
    Notification postTime :: 15464X794103
    Notification From :: com.whatsapp
    Notification TikerText :: null
    Notification Title :: XXX X Bca (2 messages): ​
    Notification Text :: XXXXX(last new Message)
    

提交回复
热议问题