有什么方法可以获取设备通知历史记录?

时间:2019-03-23 14:45:01

标签: android kotlin notifications android-notifications

我正在开发一个使用情况统计应用程序,我想知道其他应用程序如何访问设备通知历史记录

现在我正在使用NotificationListenerService,但这只能处理收到的新通知,而不能处理过去的通知。 我检查了Android DOC,发现一些“仅系统”方法,例如getHistoricalNotifications(),或需要ACCESS_NOTIFICATION权限的方法。

问题在于,实际上有一些应用程序可以访问通知历史记录数据。

Android DOC还显示了Android Q上名为NotificationStats的新API,但目前不可用。

有什么提示吗?甚至可以通过非黑客方式实现吗?

2 个答案:

答案 0 :(得分:2)

ActionDash显示此enter image description here

正如您所提到的,getHistoricalNotifications是仅系统应用程序可以访问的系统级API,因此作为系统应用程序的Google Digital Wellbeing可以显示这些信息。

因此,我认为无法获取第三方应用程序的通知历史记录。

/**
         * 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;
        }

答案 1 :(得分:0)

在将通知接收到sql数据库中时,您应该存储应用程序通知数据;当您想要各个应用程序的通知历史记录时,可以通过将查询放入sql数据库来轻松获取所有通知。