4

i'm trying to close notification when my app being closed,because when i close the app from (Recent Task / Swipe to exit),notification still appear and in case user try to click on notification the app will crash.

 @Override
    protected void onDestroy() {
        super.onDestroy();
        notification.cancel(1);
        try {
            MApplication.sBus.unregister(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

in onDestroy() but doesn't work because onDestroy() is not called everytime.

James-so
  • 125
  • 1
  • 13
ali
  • 189
  • 3
  • 14
  • have you tried notificationManager.cancelAll(); instead of cancel this will cancel all the notifications from current application context. – vikas kumar Jan 19 '18 at 18:00
  • @vikaskumar thanks for your comment but doesn't work as i say onDestroy is not called everytime ,so i can cancle my notification – ali Jan 19 '18 at 18:04
  • from where you are calling activity/fragment – vikas kumar Jan 19 '18 at 18:05
  • actually all my necessary code in MainActivity – ali Jan 19 '18 at 18:08
  • yeah, onDestroy is not sure to be called but I guess it will fulfil requirement most of the time. – vikas kumar Jan 19 '18 at 18:26
  • How about `onStop()` ? It's [guaranteed to be called](https://stackoverflow.com/questions/29395169/is-activity-onstop-guaranteed-to-be-called-api-11) for every Android version from API level 11+ – Bö macht Blau Jan 19 '18 at 18:28
  • Am I the only one thinking that the onDestroy() should be called just before the ending brace as it makes the other code unreachable? Anyone? see [here](https://stackoverflow.com/questions/22091136/should-we-call-the-super-class-before-or-after-we-execute-some-code?lq=1). – Lalit Fauzdar Sep 30 '18 at 17:29

1 Answers1

6

problem solved

Add a java class file. Here file name is KillNotificationService.java

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
public class KillNotificationService extends Service{

@Override
public void onTaskRemoved(Intent rootIntent) {
//Toast.makeText(this, “service called: “, Toast.LENGTH_LONG).show();
super.onTaskRemoved(rootIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getSystemService(ns);
nMgr.cancelAll();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

Call this service in your MainActivity oncreate method.

startService(new Intent(this, KillNotificationService.class)); 

Add service in Manifest file

<service android:name=".KillNotificationService"/>
ali
  • 189
  • 3
  • 14
  • @crgarridos try to cancel your notification by using notification id `nMgr.cancel(your_notification_id);` – ali Jan 19 '18 at 22:28
  • oups ! sorry I wanted to mean: calling from an activity – crgarridos Jan 20 '18 at 10:58
  • I originally voted up his answer after using the solution in my app I have discovered a problem with `startService(new Intent(this, KillNotificationService.class));`. After the app is closed `KillNotificationService.class` should also be killed. This means you will get an `java.lang.IllegalStateException` the next time you start the app. – goldy1992 Nov 23 '19 at 11:31