1

I want the application process to be killed when the user slides the notification off the screen or hits the clear all notifications button.

Is there a way to implement this?

Devansh
  • 141
  • 2
  • 15

1 Answers1

0

DeleteIntent: DeleteIntent is a PendingIntent object that can be associated with a notification and gets fired when the notification gets deleted.

You can set the Pending Intent to a broadcast Receiver and then perform any action you want.

Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
  Builder builder = new Notification.Builder(this):
 ..... code for your notification
  builder.setDeleteIntent(pendingIntent);

MyBroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
         //code to kill app
         c̶o̶n̶t̶e̶x̶t̶.̶f̶i̶n̶i̶s̶h̶A̶f̶f̶i̶n̶i̶t̶y̶(̶)̶;̶
         ActivityCompat.finishAffinity((Activity)context);
     }
}

reference: catch on swipe notification event

Community
  • 1
  • 1
Aman Grover
  • 1,621
  • 1
  • 21
  • 41
  • Where is `finishAffinity()` defined, the receiver does not resolve the method. I've tried the alternatives given [here](http://stackoverflow.com/questions/26134560/close-application-from-broadcast-receiver) as well but none of them are working – Devansh Sep 09 '16 at 17:58