Is it possible to react to user cancellations of android notifications? - android

I have a background service setting up a notification in Android's status bar. The background service is doing some work, but needs to stop when the user clicks on or cancels the notification.
I am able to stop the service, if the user clicks on the notification by passing an intent to the notification when creating it.
Is it possible to react if the user cancels the notification? Or if all notifications are canceled? If so, how?
NB: I define cancel as removing the notification by swiping to the right on it, or clicking on the x at the top right to remove all the notifications.

Check out the deleteIntent of the Notification:
The intent to execute when the status entry is deleted by the user with the "Clear All Notifications" button.

Related

Alternative to CLOSE_SYSTEM_DIALOGS in Android 12 to hide notification tray

I have an app that carries out a task for the user in the background (using a foreground service).
This has a notification with two action buttons, one of which starts an Activity.
In Android 11, when the user tapped on the action button that starts the Activity, I sent a broadcast using the CLOSE_SYSTEM_DIALOGS action, which would dismiss the notification tray so that the user could see the new Activity.
Apps aren't allowed to use the CLOSE_SYSTEM_DIALOGS intent action any more, but I don't see any alternative.
https://developer.android.com/about/versions/12/behavior-changes-all#close-system-dialogs
It's a really clunky experience for users to have to press the action button, then close the notification tray manually.
Is there any other way to dismiss the notification tray when the user taps an action button?

Service.startForeground() forces the notification to be ongoing, is there a workaround?

When I show a notification via service.startForeground(id, notification), the notification is set to be ongoing, which makes the app cumbersome to use. Is there a workaround to make a foreground notification non-ongoing so that it can be easily swiped away?
If not, I guess I would have to choose between using a background service or adding a "dismiss" action to the notification, which already contains some actions.

Detecting Android Wear Notification Dismiss Events

The Android Wear OS has recently been updated to 5.0, which includes the feature to "recover" the most recently dismissed notification.
As a consequence, a notification's delete intent is only triggered after the user can no longer recover the intent (after another notification is dismissed, or the user swipes down and lets the 'dismiss' timeout expire).
I am attempting to display a notification, which gets updated fairly regularly until the user dismisses it. For this, I need to know immediately when the user has dismissed the notification (before the "recover" option has expired), so that I can stop updating the notification. If I attempt to update the notification once it has been dismissed, it will launch a new notification. In effect, the notification can't be dismissed as it will be recreated immediately.
So my question is: Has anyone discovered a way to detect immediately when the user dismisses a notification on an Android Wear device, before the option to recover the notification expires? I suspect if such a method exists, it should also be possible to detect when the user recovers the notification.
How about this?
Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. The PendingIntent set with setDeleteIntent(PendingIntent) will be broadcast when the notification is canceled.
public NotificationCompat.Builder setAutoCancel (boolean autoCancel)
As I see it, it dismisses the notification immediately and it sends the setDeleteIntent() at the same time

List and delete Android notifications

Is there a way to get the list of notifications of my app?
I need to list them and let the user to delete them one by one, but I can't find a way.
I don't believe that you can list your notifications that have been shown. If the user wants to clear a notification from the app then all they need to do is pull the notification draw down and swipe it away. You can have your app cancel a notification using the NotificationManager by supplying the id of your notification to the cancel method.
If the use clicks the notification you then have two options for clearing.
1) When creating the notification you can call Notification#setAutoCancel(true) and the notification will be automatically cleared when the user taps on it.
2) Pass the notification id as part of the PendingIntent with the notification then you can use this to cancel the notification when the user taps on it.

Android Notification : perform task in place by clicking notification. Do not launch an activity

My application adds a notification in the notification bar. Now when user clicks the notification, is there a way to execute some code inplace rather than control is redirected back to my app.
Let the user be in notification area after clicking this notification and the click does the work in some background task or service.
Is it possible to send a handler message in this case
Update to question:
If I wish to have 2 buttons in the notification. where would the handling code run
That should be an easy one. Just use an IntentService:
http://developer.android.com/reference/android/app/IntentService.html
and here the first tutorial I found on google:
http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/

Categories

Resources