Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - retrieve list of old notifications

I know that on Android you can retrieve a list of current active notifications with NotificationListenerService. However is it possible to retrieve a list of old notifications (meaning not active anymore).

I know that there is a feature in the Android OS called Notification Log. Is it possible to get kind of the same content just for my application only? Or does this have to be handled on the application level to keep that kind of history?

like image 526
Jakub Holovsky Avatar asked Oct 18 '22 12:10

Jakub Holovsky


1 Answers

Unfortunately Notification Log uses the method getHistoricalNotifications of NotificationManagerService that requires ACCESS_NOTIFICATIONS permission. For this reason it is reserved to system apps:

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

The only viable option is to create a NotificationListenerService, implement the method onNotificationPosted and keep track locally about new notifications posted by apps.

like image 105
Mattia Maestrini Avatar answered Oct 21 '22 10:10

Mattia Maestrini