I listen for notifications like WhatsApp Messages.
But every time a notification comes in the NotificationListenerService fire twice.
Does anyone know this problem??
This is a snippet from the AndroidManifest.xml:
<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>
And inside the NotificationListenerService class:
public class NotifyService extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i("NotifyService", "got notification");
}
}
Edit:
Properties of both StatusBarNotification
s:
First notification:
0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073
Second notification:
0|com.whatsapp|1|null|10073
I'm not sure why this happens. Maybe flags of notifications could be triggering it twice.
You can try to omit duplicate executing yourself:
Each
StatusBarNotification
has unique key which is generated:Holding each previous key can distinguish latter notification for given package.
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
this method
databaseHandler.checkNotification(modifiyedUniq!!)
returns true if record present with this key else it returns falseeach and every time checking the key if no record present means its new notification
Notification
0|com.whatsapp|1|XX2X606878@s.whatsapp.net|10171
tag =
91XX06X78@s.whatsapp.net
Using Split you can achieve this thing.