Reputation: 3972
I have an app with an activity and an intent service running in the background.
There's also a notification which shows a progress bar with the progress of the background process (it's downloading a lot of files from a server).
The activity shows the download progress ( user can't meaningfully use the app before these assets are downloaded). When this activity is closed, the notification with the progress bar is shown.
My issue: When the application is killed via the "task manager" (accessible via right button on android 4.0, the one with the two rectangles), the notification is still there. But it's basically a zombie, because the service that used to update it is dead.
Put in a different way: How can I remove a (or all) notification(s) when my application is killed?
Upvotes: 15
Views: 19570
Reputation: 263
you can write the code to remove the notification in this callback method of the service class. and if else condition clear rest of the thing.
override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true)
}else{
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager .cancelAll()
}
}
Upvotes: 0
Reputation: 330
use in your notification builder the setTimeoutAfter
and then use a recursive call that starts a coroutine to refresh the notification before it cancels itself.
This way, if your service is killed (which won't go through onDestroy or receive any other callbacks to trigger cancellation of the notification) the notification simply times out for you. And the only way a service that is started + bound will be stopped is when the OS kills the application after being idle.
not using a foreground service also means that your normal application lifecycle won't be inhibited. It's tricky to get right, but doable.
see methods buildNotification
and notify
, and launchRefreshNotificationJob
from https://github.com/05nelsonm/TorOnionProxyLibrary-Android/blob/master/topl-service/src/main/java/io/matthewnelson/topl_service/notification/ServiceNotification.kt
Upvotes: 0
Reputation: 21899
@See
public void onTaskRemoved(Intent rootIntent).
This is called if the service is currently running and the user has removed a task that comes from the service's application. If you have set ServiceInfo.FLAG_STOP_WITH_TASK
flag then you will not receive this callback; instead, the service will simply be stopped.
You can remove all notifications that your app has created with cancelAll()
:
NotificationManager nManager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
nManager.cancelAll();
According to the API docs, this will
Cancel all previously shown notifications.
in the same way that cancel(int id)
does:
Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.
Upvotes: 27
Reputation: 553
You can not track if application is killed from Activity or Application class.
I found a solution for this in the following link : Cancel notification on remove application from multitask panel
Please try this.
Upvotes: 0
Reputation: 937
Did you tried onStop ? It worked for me. I called cancelAll() in onStop. It closes all notification on removal of app from task Manager.
Note : But it also remove all notification when your app goes in background. In that case you can re invoke those notification using background process.
@Override public void onStop (){
super.onStop();
cancelAllNotification(getApplicationContext());
}
public static void cancelAllNotification(Context ctx) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancelAll();
}
Upvotes: -4
Reputation: 3972
It seems that
of the Service DOES get called when the user kills the application task (by swiping out the app in the "task manager". I am able to remove the Notification from here.
Upvotes: 4