【问题标题】:Android onNotificationPosted is called twice for gmail and whatsAppgmail和whatsApp调用Android onNotificationPosted两次
【发布时间】:2017-08-25 23:19:23
【问题描述】:

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

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

它适用于 SMS 和环聊通知(即每个通知只触发一次)但是当我使用 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()); }
       */

   }
}

【问题讨论】:

  • 事实上,每条Gmail通知都有不同的ID,而且发布的时差非常小。

标签: java android notifications notification-listener


【解决方案1】:

发生这种情况是因为 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;
          }

          //...
   }

【讨论】:

  • 我假设 Android 10 具有更多的安全功能以及通知访问权限。一点也不奇怪,dit 可能会被永久限制。
【解决方案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();
    }
  }

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

希望对您有所帮助。

【讨论】:

  • 谢谢@Andre Breton,我要试试。一些论坛含糊地暗示,Gmail 通知可能会处理两次,一次在发布时处理,另一次在准备显示时处理。但目前还没有人确定这一点。
【解决方案3】:

您可以通过将收到的 Notification.when 与最后一个 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...

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多