Android - stay on notification list - android

I'm building an Android app which creates notifications. Each notification has a number of additional actions.
The default application (when the user taps on the notification) launches an activity in my app. That's working fine.
When the user swipes the notification to dismiss the notification, my app responds by doing some deletion work, and the user is left on the list of notifications. The one that the user swiped is removed from the list.
For some of my additional actions I want to launch activities, and that's working fine; for the other actions, I want my app to do some work but I also want the user to remain on the notification list. I'm struggling to find a way to do this last part - all my attempts to configure the intent and pending intent for my additional action result in behaviour whereby when the user clicks on the action, the notification list disappears.
Is there any way to get what I want (i.e. a notification additional action which performs work but leaves the user on the notification list)? Do I need to build a custom notification layout or something?

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?

Summary of notification group is not automatically dismissed when child notifications are programatically canceled

I have been struggling a lot with notifications lately.
Notifications can be easily dismissed after being clicked by using
notificationBuilder.setAutoCancel(true);
Summary notifications are required for grouping those individual notifications. Whenever child notifications are present, the summary notification is not displayed as is (titles, texts and actions are ignored, for example). When all have been individually dismissed, then the summary notification is fully displayed, with its texts and actions. This feels weird: why would we need a summary after having seen all notifications individually? Who knows... Anyway, we can get rid of this unexpected behaviour using
summaryNotificationBuilder.setAutoCancel(true);
Now, when the their action has been clicked, notifications do not automatically dismiss even though notificationBuilder.setAutoCancel(true) has been called. As explained in How to dismiss notification after action has been clicked, we need to keep track of their unique id we have provided when displaying the notification
notificationManager.notify(uniqueNotificationId, notificationBuilder.build());
From the receiving activity or broadcast receiver, we would call
notificationManager.cancel(uniqueNotificationId);
Let's be honest: this is horrible.. but it seems to be the way to go. I haven't been able to find any good alternative. Now, dismissing notifications like previously stated seems to interfere with summaryNotificationBuilder.setAutoCancel(true). When I individually dismiss all child notifications by clicking them or swiping them away, the summary notification is automatically cancelled. But when I trigger an action from any of those (thus triggering the mechanism of creating an Intent passing an extra uniqueNotificationId, and then calling NotificationManagerCompat.from(context).cancel(uniqueNotificationId) from the receiving activity or broadcast receiver, then the summary notification is not automatically canceled, even though summaryNotificationBuilder.setAutoCancel(true) was used.
Any idea on how to fix this?
I didn't find a way to programmatically figure out how many child notifications are still visible in order to also manually dismiss the summary notification, for example. Edit: Actually, now I'm trying to unconditionally dismiss the summary notification... without success.

Deciding which activity to open on notification click

I'm currently trying to find out, how to specify the destination activity on click on an android notification - but not until actually clicking the notification.
I have the following use case: I have an app where the user has to log in before using it. So if the user receives a notification from my app while he is authenticated and using the app in foreground, a click on the notification should lead to lets say ExampleActivity. But if the app is in background and a notification is received, a click should redirect to LoginActivity, where the user has to authenticate first.
Since the destination activity is specified when creating and setting the Intent to the NotificationCompat.Builder, I have no later control over the destination activity.
So if the user is logged in and receives a notification, closes the app and clicks on the notification, he gets redirected to ExampleActivity, even though he would have to log in beforehand, which is a security flaw for my process.
I feel like, there should be a common approach on how to solve this kind of problem. Is there a possibility to check if the app is running during click on the notification and decide where to redirect the user to?

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.

Expanding a notification from the notification bar

Is it possible to make a notification expand on click? For example, by clicking on the notification it would bring up a listview with more options.
In the app I'm working on, a user has many files. Each time the app is opened a notification is issued with the number of files that have been modified. Is there any way to click on this notification so I can see a sub-list of each file?
I could do this by registering an intent with a ListActivity, but is there a cleaner way to do it?
Thanks!

Categories

Resources