我知道这看起来像是Android NotificationListenerService onNotificationPosted fire twice和NotificationListenerService 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()); }
*/
}
}
答案 0 :(得分: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();
}
}
在旅途中写下来,可能需要检查代码
希望它有所帮助。
答案 1 :(得分:2)
之所以会发生这种情况,是因为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 :(得分:0)
您可以通过比较收到的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...
}