Cancel Notification on Remove Application from Multitask Panel

Cancel notification on remove application from multitask panel

Killing Notifications when main app has been killed.

Since your notification and your app are handled in different threads killing your app via MultitaskManager won't kill your notification. As you already correctly investigated killing your app won't even necesarrily result in an onExit() callback.

So what is the solutions?

You could start a service from your activity. A specialty services have: they restart themselves automatically if app-process have been killed for some reason. So you could reuse the automatic restart by killing the notification on restart.

Step 1 create a service that kills
Simple one. It just kills a notification on create and has his special Binder.

public class KillNotificationsService extends Service {

public class KillBinder extends Binder {
public final Service service;

public KillBinder(Service service) {
this.service = service;
}

}

public static int NOTIFICATION_ID = 666;
private NotificationManager mNM;
private final IBinder mBinder = new KillBinder(this);

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
@Override
public void onCreate() {
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNM.cancel(NOTIFICATION_ID);
}
}

Step2: Add it to your manifest:
Add it somewhere inbetween your <application> tags.

<service android:name="KillNotificationsService"></service>

Step3: Always create the Service before fireing the notification, and use the static notificationid.

ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder binder) {
((KillBinder) binder).service.startService(new Intent(
MainActivity.this, KillNotificationsService.class));
Notification notification = new Notification(
R.drawable.ic_launcher, "Text",
System.currentTimeMillis());
Intent notificationIntent = new Intent(MainActivity.this,
Place.class);
PendingIntent contentIntent = PendingIntent.getActivity(
MainActivity.this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(),
"Text", "Text", contentIntent);
NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNM.notify(KillNotificationsService.NOTIFICATION_ID,
notification);
}

public void onServiceDisconnected(ComponentName className) {
}

};
bindService(new Intent(MainActivity.this,
KillNotificationsService.class), mConnection,
Context.BIND_AUTO_CREATE);

It might take a little time until service is restarted (1-5 sec), but it will eventually start and kill the notification.

How to remove all notifications when an android app (activity or service) is killed?

@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.

Cancel notification after application is closed from background

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.

Remove notification icon of killed foreground service (Android)

Based on the answers here I found a solution (thx @Anubhav Gupta).

You can override the onTaskRemoved function in which the foreground service can be stopped.

@Override
public void onTaskRemoved(Intent rootIntent) {
Intent intent = new Intent(this, MyService.class);
context.stopService(intent);
}


Related Topics



Leave a reply



Submit