Java Android onNotificationPosted为gmail和whatsApp调用了两次

Java Android onNotificationPosted为gmail和whatsApp调用了两次,java,android,notifications,notification-listener,Java,Android,Notifications,Notification Listener,我知道这看起来像是和的复制品,但我尝试过他们的解决方案,但似乎不起作用 我所要做的就是提供一个后台服务,计算一个人在电话上收到的通知数量,然后将其写入一个文本文件 它适用于SMS和Hangout通知(即每次通知只触发一次),但当我使用WhatsApp和Gmail测试它时,它会触发两次,因此,对于每次Gmail通知,我的文本文件中有两行 这是我的密码。任何帮助都将不胜感激 public class NotifCounterService extends NotificationListenerSe

我知道这看起来像是和的复制品,但我尝试过他们的解决方案,但似乎不起作用

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

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

   }
}

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

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();
    }
  }
在运行时编写,可能需要检查代码


希望能有帮助

之所以会出现这种情况,是因为WhatsApp和Gmail会在发送其他通知的同时发送组摘要通知

此处记录了相关标志:

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

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

          //...
   }

您可以通过比较收到的通知.when与上次通知.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...

}
private final Map pkgLastNotificationWhen=new HashMap();
@凌驾
公告发布时的公共作废(状态公告sbn){
if((sbn.getNotification().flags&Notification.FLAG\u GROUP\u SUMMARY)!=0){
Log.d(标记“忽略通知标志\u组\u摘要”);
返回;
}
Long lastTime=pkgLastNotificationWhen.get(sbn.getPackageName());
if(lastWhen!=null&&lastWhen>=sbn.getNotification().when){
Log.d(标记“忽略旧通知”);
返回;
}
pkgLastNotificationWhen.put(sbn.getPackageName(),sbn.getNotification().when);
//做点什么。。。
}

事实上,每个Gmail通知都有不同的ID和一分钟的发布时差。谢谢@Andre Breton,我将尝试一下。一些论坛模糊地暗示Gmail通知可能会被处理两次,一次是在发布时,另一次是在准备显示时。但目前还没有人能确定这一点。我假设安卓10有更多的安全功能,可以访问通知。dit可能受到永久性限制并不奇怪。