Detect if notification has been deleted

2020-07-23 03:31发布

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.

4条回答
SAY GOODBYE
2楼-- · 2020-07-23 04:06

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.

查看更多
够拽才男人
3楼-- · 2020-07-23 04:08

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

查看更多
Evening l夕情丶
4楼-- · 2020-07-23 04:13

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

查看更多
虎瘦雄心在
5楼-- · 2020-07-23 04:14

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)

查看更多
登录 后发表回答