Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onNotificationPosted is called twice for gmail and whatsApp

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()); }
       */

   }
}
like image 808
RforResearch Avatar asked Aug 25 '17 23:08

RforResearch


People also ask

Why am I getting 2 notifications from GMail?

Thanks! GMail and Inbox are two different apps - they both receive the same email (assuming you have the same accounts in each). If you have notifications enabled in each, they'll each notify you when they receive email (which will be when the email arrives at your phone).

What is android listener service?

A service that receives calls from the system when new notifications are posted or removed, or their ranking changed.

What is notification listener in android?

A notification listener service allows the Google App to intercept notifications posted by other applications. Notification Options in the Google App Notification Listener Services. Within the Android Manifest file is the inclusion of the new Notification Listener Service.


1 Answers

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;
          }

          //...
   }
like image 105
Jrs42 Avatar answered Oct 03 '22 06:10

Jrs42