如何在 android onNotificationPosted() 上获取通知时间戳

how to get timestamp of notification on android onNotificationPosted()

我正在 public void onNotificationPosted(StatusBarNotification sbn) 方法中解析在 android 中发布的什么应用程序通知。问题是每当 whats app 发布新的 whats app 通知时,所有旧的未读通知都会重新发布到 onNotificationPosted() 方法。所以我最终再次解析相同的通知。 Notification对象中有没有像时间戳这样的唯一值,所以我可以知道我是否已经解析了那个通知。

下面是我使用的源代码:

 public void onNotificationPosted(StatusBarNotification sbn,NotificationListenerService.RankingMap rankingMap) {
        if(isWhatsAppNotification(sbn.getPackageName())) {

          Bundle extras = sbn.getNotification().extras;
          String title = extras.getString("android.title");
          String text = extras.getCharSequence("android.text").toString();

            Log.i("Text2222 Text", text);
            Log.i("Text2222 Title", title);
            }
           }

有sbn.getPostTime?

发布通知的时间(System#currentTimeMillis 时间),可能与 Notification.when 不同。

来自 https://developer.android.com/reference/android/service/notification/StatusBarNotification#getPostTime()