Android NotificationListenerService onNotificationPosted fire twice
I'm not sure why this happens. Maybe flags of notifications could be triggering it twice.
You can try to omit duplicate executing yourself:
public class NotifyService extends NotificationListenerService {
private String mPreviousNotificationKey;
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
if(TextUtils.isEmpty(mPreviousNotification) || !TextUtils.isEmpty(mPreviousNotification) && !sbn.getKey().equals(mPreviousNotificationKey)){
Log.i("NotifyService", "got notification");
}
}
Each StatusBarNotification
has unique key which is generated:
private String key() {
return user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid;
}
Holding each previous key can distinguish latter notification for given package.