Detecting programmatic removal of Android notifications in NotificationListenerService

35 Views Asked by At

I'm working on an Android app that uses the NotificationListenerService to monitor notifications posted on the device. I'm trying to determine when a notification is removed by the app itself (programmatically), as opposed to being removed by the user or by the system. Is there a way to do this in the NotificationListenerService?

I've tried using the onNotificationRemoved(StatusBarNotification sbn) method, but it doesn't seem to provide any information about the cause of the removal. Even if sbn.isClearable() returns true, the notification can still be removed by the app itself. I'm wondering if there's some other way to distinguish between user-initiated removal and programmatic removal of notifications.

Any help or guidance would be greatly appreciated. Thank you!

Here my code, its working only for non clearable notificatoins.

public class MyNotificationListenerService extends NotificationListenerService {
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    if (!sbn.isClearable()) {
        // Notification was removed by the program
        Log.d("MyNotificationListener", "Notification removed by app: " + sbn.getPackageName());
    } 
}
}
0

There are 0 best solutions below