对于 gmail 和 WhatsApp,Android onNotificationPosted 被调用两次 [英] Android onNotificationPosted is called twice for gmail and whatsApp

查看:145
本文介绍了对于 gmail 和 WhatsApp,Android onNotificationPosted 被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这看起来像 Android NotificationListenerService onNotificationPosted 触发两次NotificationListenerService onNotificationPosted() 当它是分组通知的一部分时为单个通知多次调用,但我尝试了他们的解决方案,但似乎不起作用.

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.

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

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

   }
}

推荐答案

发生这种情况是因为 WhatsApp 和 Gmail 会在发送其他通知的同时发送群组摘要通知.

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

相关标志记录在此处: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;
          }

          //...
   }

这篇关于对于 gmail 和 WhatsApp,Android onNotificationPosted 被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆