对于gmail和whatsApp,Android onNotificationPosted被调用两次

问题描述 投票:0回答:2

我知道这看起来像Android NotificationListenerService onNotificationPosted fire twiceNotificationListenerService onNotificationPosted() called multiple times for single Notification when it is part of grouped notification的重复,但我已经尝试了他们的解决方案,他们似乎没有工作。

我想做的就是拥有一个后台服务,计算一个人在手机上收到的通知数量,然后将其写入文本文件。

它适用于短信和环聊通知(即每次通知只会触发一次)但是当我使用WhatsApp和Gmail测试它时会被触发两次,因此,对于每个Gmail通知,我的文本文件中都有两行。

这是我的代码。任何帮助将不胜感激。

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

   }
}
java android notifications notification-listener
2个回答
3
投票

这是因为WhatsApp和Gmail会发送群组摘要通知以及其他通知。

相关标志记录在这里:https://developer.android.com/reference/android/app/Notification.html#FLAG_GROUP_SUMMARY

您可以忽略此标志的通知,如下所示:

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

          //...
   }

2
投票

您的代码看起来不错,我认为更有可能是这个应用处理通知的方式,如果应用尝试同时创建多个通知,您可以做的是创建一个标志:

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

在旅途中写下来,可能需要检查代码

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.