Do I need to persist my Notification IDs - android

I have an alarm application where users can set multiple alarms. When an alarm goes off the app simply displays a Notification which when clicked will launch the main activity screen and remove the Notification.
Each alarm gets its own Notification which is where my question comes in; if there are multiple Noitifications showing, when the user clicks one I would like to clear all of my app's Notifications. To do this I need to track the IDs of the Notifications currently showing. What is the recommended way of doing this?
I suppose these will need to be persisted as my app's process could be killed as soon as a Notification has been created and displayed. Is my thinking correct?

To do this I need to track the IDs of the Notifications currently showing.
Or just call cancelAll() on NotificationManager, to cancel all your outstanding notifications.
What is the recommended way of doing this?
To be safe, you should use some persistent data store (e.g., file), as your process might go poof between alarms.
I suppose these will need to be persisted as my app's process could be killed as soon as a Notification has been created and displayed. Is my thinking correct?
It could certainly be killed before the next alarm, as discussed in your other recent question.

Related

Check if "setFullScreenIntent" leads to a launch of the application or to the display of a notification

I get a firebase notification and create a NotificationCompat.Builder with the "setFullScreenIntent" option. This leads to different behaviours on different systems or different device status.
Sometimes the app launches directly, sometimes a notification is displayed.
I have to surpress a ringtone if the user clicked on the notification but play it when the app starts directly.
How do I get to know which one happened when my activity starts?
Sorry for my English.
You can do cancel the Notification in activity#onCreate or other lifecycle where you want

Callback from opening of Android notification drawer

Is there any way I can register one of my own activities, so it gets invoked whenever the notification drawer is opened? I know full well how to get my activity started when the user taps on my notification, but that's not what I want. I want my activity to run earlier -- at the moment the user pulls down the notification drawer and merely reveals my notification.
As you might imagine, I want my activity to update the notification's text (i.e. call NotificationBuilder.setContentText()).
It must be possible. For example, the 3G Watchdog app does this sort of thing (it has the notification show how much bandwidth you've used this month). I imagine this can be done if I write an app that can never die -- a daemon of sorts -- but I understand that's considered bad form in Android apps.

Swipe away my application from the recents activity kills my service

I created a service to handle my AsyncTasks like uploading a file on a server or downloading one. When I swipe away my app from the recent activity menu, my service is killed. Is it normal behaviour ? If so, one solution would be to set it as a foreground service with startForeground(int, Notification) but I must display a notification and I don't want it as I'm already displaying one for each AsyncTask running.
How does the "play store" app download applications and keep the downloads alive even if I swipe away the "play store" from the recent activity menu ?
Is it normal behaviour ?
Yes. Android, at the user's request, terminated your background process.
If so, one solution would be to set it as a foreground service with startForeground(int, Notification) but I must display a notification and I don't want it as I'm already displaying one for each AsyncTask running.
Please do not show a separate Notification "for each AsyncTask running". At most, show one Notification. Few, if any, apps are important enough to warrant separate Notifications.

Get all existing notifications from system tray

Is it possible for me to get all of the notifications from my app that's currently in the system tray?
I noticed we can update existing notifications that are already in the tray, but I am wondering if it's possible for me to retrieve all of them.
http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating
Thanks
The short answer is "No", the Notification Manager does not provide a way to query the notifications currently in the tray.
If you want to keep track of your notifications in the tray, it is possible, but will take a little bit of work. You will need to create a way to track the status of your notifications, the easiest way is probably a database. What you do is insert a row for the notification, with that notification's id, when you create the notification. Next, you need to build an Activity or Service that will update your database with the status of your notification during its lifecycle. Before posting your notification to the manager, you need to add a contentIntent and a deleteIntent that route through the Activity or Service you just created. The contentIntent will be called when the user clicks the notification, and the deleteIntent will be called if the user dismisses it. When either of those happen, update your database to track the latest state!
If you implement that, you'll have a way to track your notifications, and their status in the tray. Unfortunately, there is no easy, readily available way to query them, especially if they're not of your creation.
Try this NotificationListenerService:
https://developer.android.com/reference/android/service/notification/NotificationListenerService

How long will a non-clearing notification stay?

If my app fires a notification and marks it as non-clearing (i.e. tapping it does not remove it), what will result in the notification being removed?
Would I be right in assuming that if the app is shut down by the memory manager that this will cause the notification will be removed? Or will the notification persist even if the app is recycled?
My app needs to show a persistent notification, and I need to understand whether I can set it up in the app, or if I need to build a service to manage it.
When you start a notification with the FLAG_NO_CLEAR it will persist until you cancel it in the NotificationManager with a call to NotificationManager.cancel(int notificiationId)
I'm not sure what would happen to the notification if your app were uninstalled, although I suspect it would be cleared then too. That said it should be really easy to test. Create an activity that creates a notification. Then close it, kill it, uninstall it, etc... and see what happens.

Categories

Resources