7

I know this looks like a duplicate of Android NotificationListenerService onNotificationPosted fire twice and NotificationListenerService onNotificationPosted() called multiple times for single Notification when it is part of grouped notification, but I have tried their solutions and they don't seem to work.

All I want to do is to have a background service that is counting the number of notifications a person gets on the phone and then just write that into a text file.

It works fine for SMS and Hangout notifications (i.e. it is fired only once for each notification) but when I test it using WhatsApp and Gmail, it gets fired twice and so, for each Gmail notification, I have two rows in my text file.

Here is my code. Any help will be appreciated.

public class NotifCounterService extends NotificationListenerService {

   public static String TAG = NotifCounterService.class.getSimpleName();

   Date dateStart;

   private Logger logger;

   private Context mContext;

   @Override
   public void onCreate() {
      Log.d(TAG, "Created");

      logger = new Logger(TAG);
      mContext = getApplicationContext();
   }

   @Override
   public IBinder onBind(Intent intent) {
      return super.onBind(intent);
   }

   @Override
   public void onNotificationPosted(StatusBarNotification sbn) {

      Log.d(TAG, "Notification has arrived");

      Log.d(TAG, "ID: " + sbn.getId() + " Posted by: " + sbn.getPackageName() + " at: " + sbn.getPostTime() + " ");

      logger.i(sbn.getId() + "," + sbn.getPackageName() + "," + sbn.getPostTime(), mContext);
      logger.close();

      /*
       * Log.i(TAG, "ID:" + sbn.getId()); Log.i(TAG, "Posted by:" +
       * sbn.getPackageName()); Log.i(TAG, "tickerText:" +
       * sbn.getNotification().tickerText);
       */

      /*
       * for (String key : sbn.getNotification().extras.keySet()) { Log.i(TAG, key +
       * "=" + sbn.getNotification().extras.get(key).toString()); }
       */

   }
}
DPM
  • 1,960
  • 3
  • 26
  • 49
RforResearch
  • 401
  • 8
  • 16

4 Answers4

15

This happens because WhatsApp and Gmail send a group summary notification alongside other notifications.

The related flag is documented here: https://developer.android.com/reference/android/app/Notification.html#FLAG_GROUP_SUMMARY

You can ignore notifications with this flag like this:

   @Override
   public void onNotificationPosted(StatusBarNotification sbn) {
          if ((sbn.getNotification().flags & Notification.FLAG_GROUP_SUMMARY) != 0) {
                  //Ignore the notification
                  return;
          }

          //...
   }
Jrs42
  • 456
  • 4
  • 6
  • I am assuming Android 10 has more security features with notification access. Not surprised a dit may be permanently restricted. – RforResearch Oct 09 '19 at 02:56
4

Your code looks good, i think is more likely the way this apps handle notifications, what you can do is to create a flag if the app tries to create multiple notifications at the same time:

public void timeCheck(){
 if(timeCheck = true){
   timeCheck = false;
     new CountDownTimer(2000, 1000) {
       public void onTick(long millisUntilFinished) {

       }

       public void onFinish() {
         timeCheck = true;
         notificationSamePackage = "";
       }
    }.start();
  }
}

......
......

   @Override
   public void onNotificationPosted(StatusBarNotification sbn) {
      if (notificationFromSamePackage != sbn.getPackageName() && timeCheck){
      Log.d(TAG, "Notification has arrived");
      Log.d(TAG, "ID: " + sbn.getId() + " Posted by: " + sbn.getPackageName() + " at: " + sbn.getPostTime() + " ");
      logger.i(sbn.getId() + "," + sbn.getPackageName() + "," + sbn.getPostTime(), mContext);
      logger.close();
      notificationSamePackage = sbn.getPackageName();
      timeCheck();
    }
  }

Wrote it on the go, code might need to be checked

Hope it helps.

Andre Breton
  • 1,273
  • 1
  • 9
  • 19
  • Thanks @Andre Breton, I am going to try it out. Some forums vaguely suggest that Gmail notifications are probably processed twice one when posted and one when ready for display. But none are sure of this as of now. – RforResearch Aug 31 '17 at 20:31
1

You can filter old notification by compare the received Notification.when with the last Notification.when.

private final Map<String, Long> pkgLastNotificationWhen = new HashMap<>();

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    if ((sbn.getNotification().flags & Notification.FLAG_GROUP_SUMMARY) != 0) {
        Log.d(TAG, "Ignore the notification FLAG_GROUP_SUMMARY");
        return;
    }

    Long lastWhen = pkgLastNotificationWhen.get(sbn.getPackageName());
    if(lastWhen != null && lastWhen >= sbn.getNotification().when){
        Log.d(TAG, "Ignore Old notification");
        return;
    }
    pkgLastNotificationWhen.put(sbn.getPackageName(), sbn.getNotification().when);

    //do something...

}
maonanyue
  • 475
  • 1
  • 3
  • 11
0

save notification when recieved with last_row_id as string in bundle after that add it to database and for further notifications process them if that is not saved in the database and repeat even repeated calls will save unique notifications

Rahim Virani
  • 106
  • 2