Android - 检索旧通知列表

Android - retrieve list of old notifications

我知道在 Android 上,您可以使用 NotificationListenerService 检索当前活动通知的列表。但是,是否可以检索旧通知列表(意味着不再有效)。

我知道 Android OS 中有一个名为 通知日志 的功能。是否可以仅为我的应用程序获取相同的内容?还是必须在应用程序级别处理才能保留这种历史记录?

不幸的是 NotificationManagerService 的通知日志 uses the method getHistoricalNotifications 需要 ACCESS_NOTIFICATIONS 权限。因此,它保留给系统应用程序:

/**
 * System-only API for getting a list of recent (cleared, no longer shown) notifications.
 *
 * Requires ACCESS_NOTIFICATIONS which is signature|system.
 */
@Override
public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) {
    // enforce() will ensure the calling uid has the correct permission
    getContext().enforceCallingOrSelfPermission(
            android.Manifest.permission.ACCESS_NOTIFICATIONS,
            "NotificationManagerService.getHistoricalNotifications");

    StatusBarNotification[] tmp = null;
    int uid = Binder.getCallingUid();

    // noteOp will check to make sure the callingPkg matches the uid
    if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
            == AppOpsManager.MODE_ALLOWED) {
        synchronized (mArchive) {
            tmp = mArchive.getArray(count);
        }
    }
    return tmp;
}

唯一可行的选择是创建一个 NotificationListenerService,实施方法 onNotificationPosted 并在本地跟踪应用发布的新通知。