Detect if notification has been deleted

2020-07-23 03:43发布

问题:

I have the following problem. I want to detect when a notification has been removed from the notification bar.

I can scan notifications with an AccessbilityService and casting its ParcelableData to a notification

Notification notif = (Notification) event.getParcelableData();

One thought of mine was, utilizing if the PendingIntent of the notification has been clicked, but I can't find anything on how to detect that.

Any ideas? I am kind of desperate... trying it for hours now.

回答1:

A very similar question has already been answered here on SO:

Detect a new Android notification

In short, it's possible to detect when a new notification appears, but that's about it. You can't detect when notifications have been dismissed.



回答2:

You can use a setDeleteIntent on the Notification.Builder:

http://developer.android.com/reference/android/app/Notification.Builder.html#setDeleteIntent(android.app.PendingIntent)

It can be use to do what you want (I guess it's too late for you but it can help someone else)



回答3:

as far as i know, this is impossible unless you have root, but starting from android 4.3 , which was released just yesterday, it is possible. here are some links:

android developers blog:

Notification access — Your apps can now access and interact with the stream of status bar notifications as they are posted. You can display them in any way you want, including routing them to nearby Bluetooth devices, and you can update and dismiss notifications as needed.

android developer website:

Android 4.3 adds a new service class, NotificationListenerService, that allows your app to receive information about new notifications as they are posted by the system.

If your app currently uses the accessibility service APIs to access system notifications, you should update your app to use these APIs instead.

android developer website - API description:

Access to notifications

Notifications have long been a popular Android feature because they let users see information and updates from across the system, all in one place. Now in Android 4.3, apps can observe the stream of notifications with the user's permission and display the notifications in any way they want, including sending them to nearby devices connected over Bluetooth.

You can access notifications through new APIs that let you register a notification listener service and with permission of the user, receive notifications as they are displayed in the status bar. Notifications are delivered to you in full, with all details on the originating app, the post time, the content view and style, and priority. You can evaluate fields of interest in the notifications, process or add context from your app, and route them for display in any way you choose.

The new API gives you callbacks when a notification is added, updated, and removed (either because the user dismissed it or the originating app withdrew it). You'll be able to launch any intents attached to the notification or its actions, as well as dismiss it from the system, allowing your app to provide a complete user interface to notifications.

and of course, the API documentation of this new listener, in this link



回答4:

This is possible if you extend the NotificationListener and do your own stuff in the callbacks: https://developer.android.com/reference/android/service/notification/NotificationListenerService.html