【发布时间】:2017-08-25 23:19:23
【问题描述】:
我知道这看起来像是 Android NotificationListenerService onNotificationPosted fire twice 和 NotificationListenerService 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