Android - 检索旧通知列表

标签 android android-notifications

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

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

最佳答案

不幸的是通知日志 uses方法 getHistoricalNotifications NotificationManagerService 需要 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并在本地跟踪应用发布的新通知。

关于Android - 检索旧通知列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36235481/

相关文章:

java - Android - 如何在不使用内容提供商的情况下将文件附加到邮件

未收到带有 Firebase 云消息传递的 Android 后台通知

android - 如何使用自定义 View 创建通知,但具有原生外观?

android - 使我的通知可点击

java - Android ArrayAdapter NullPointerException getID

java - 为什么我无法从 tcp 服务器获取响应?

android - SQLite 使用 ContentValues 和 BigInteger 数据类型?

android - onCreate() 在权限设置更改后从最近的任务重新打开时被调用

java - 如何在 onDestroy 函数中取消通知

java - 为什么我的通知在 Android > 7 上不起作用?