6

I have an activity that shows a notification on notification bar. If the user goes to the background pressing the home button I keep the notification (that's what I need), but when the user closes the application I need to cancel the notification and that's my problem.

  • I tried to cancel notification on onDestroy but it´s not fired.
  • I tried to set setOngoing true for the notification.

How could I deal with this? Every time the user goes background and then close the app I need to remove the notification from the notification bar, but if the user keep the application in background I need the notification.

Montag451
  • 1,168
  • 3
  • 14
  • 30
user2494863
  • 451
  • 7
  • 17

5 Answers5

2

You have your notification an ID when your created it, right?

Then all you have to do is use that ID to cancel it.
For example:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(4);

on destroy is fired when they back out of your app or if you call finish() on your activity.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • 2
    onDestroy is not called everytime. It´s up to android system to call it or not. I´m using this exactly code to cancel the notification when onDestroy is called, but it´s not called everytime. What else can I do? – user2494863 Aug 30 '13 at 16:47
  • 1
    why not use `onPause` – kabuto178 Aug 30 '13 at 16:54
  • when you call finish on an activity onDestroy is called every time – tyczj Aug 30 '13 at 16:55
  • 2
    @kabuto178 onPause is called everytime the user leaves the activity weather it be hitting the home button or backing out of the app. so if he cancels his notification in onPause then it will be canceled when the use hits the home button which is something he does not want to happen – tyczj Aug 30 '13 at 17:02
  • you can also override the onFinish method and do you cancel there which will get fired when you manually call finish() on your activity to quit the app. you really didnt specify what you consider "quitting" – tyczj Aug 30 '13 at 17:06
  • quitting for my case is when user long press the home button and then remove the aplication from the opened list. But when the user does that, no methods are fired from my activity. – user2494863 Aug 30 '13 at 17:17
  • because that is not a quit, all the user is doing is removing the app from the recent app list and does not effect your application any. if you want the user to "quit" you app you need to add a menu option or something in your app that calls finish on your activity, that would be a proper quit – tyczj Aug 30 '13 at 17:18
  • 1
    that´s not a proper quit? All my threads from the activity stop when I do this. Don´t have any way to check this behavior? – user2494863 Aug 30 '13 at 17:32
  • not that I am aware of, the only thing I found on the issue is this link http://stackoverflow.com/questions/16238628/what-happens-when-you-swipe-an-app-out-of-the-recent-apps-list-on-android which says its the same as using `killBackgroundProcesses` which will not call onDestroy when used. If you call that in your activity it still runs but stops everything else like you said – tyczj Aug 30 '13 at 17:52
  • take a look at this question, they seem to be trying to solve your exact problem. He removed his app from the recents list http://stackoverflow.com/questions/18177467/how-to-overide-on-swipe-from-recents-list-in-android – tyczj Aug 30 '13 at 17:59
  • I created an async task that runs forever and now when I remove the application from recent list, the onDestroy is called :)! crazy! – user2494863 Aug 30 '13 at 18:30
1

I created an async task that runs forever and now when I remove the application from recent list, the onDestroy is called.

Chand Priyankara
  • 6,739
  • 2
  • 40
  • 63
Sefa Aras
  • 11
  • 2
0

Notification can be canceled if we canceled it in onTaskRemoved() method,do mNotificationManager.cancel(NOTIFICATION_ID) in that method, because onDestroy method is not called every time. It's working fine for me.

Sandeep Sankla
  • 1,250
  • 12
  • 21
0

You need to create a service, then Override this method; and for ensure copy same to onCreate method, because the service may restart when this is closed.

@Override 
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancelAll();
}
Laurel
  • 5,965
  • 14
  • 31
  • 57
a442509097
  • 69
  • 3
  • This will no longer work on API level >=26 because of background services limitation - https://developer.android.com/about/versions/oreo/background#service‌s This solution is no longer viable, this service will be stopped by system after a while (so it will miss onTaskRemoved event) – Vadim Kotov Jan 11 '19 at 15:48
0

It may sounds a weird approach to solve this problem, but it worked for me, and it is applicable to all versions of android. If you have a cursor loader anywhere in you application the method onLoaderReset will be called every time the app is closed.

@Override
public void onLoaderReset(Loader<Cursor> loader) {

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancelAll();

    myRecyclerAdapter.swapCursor(null);

}
Nasser Ghodsian
  • 214
  • 3
  • 3