3

How can I react on notification dismiss? I need to kill my background process when a certain notification is cancelled.

In this thread it says use a broadcast receiver, but there is no intent-filter for notifications

Notification deleteIntent does not work

Community
  • 1
  • 1
Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136

1 Answers1

8

You don't need an intent-filter to get a broadcast if you define the Intent to trigger your class by hand.

Just implement the BroadcastReceiver

public class NotificationDeleteReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // kill service here
    }
}

And add the Receiver to the AndroidManifest.xml

<receiver android:name="NotificationDeleteReceiver" />

Then set the PendingIndent to the notification:

notification.deleteIntent = PendingIntent.getBroadcast(
    this, 0, new Intent(context, NotificationDeleteReceiver.class), 0);

This will work.

flx
  • 14,146
  • 11
  • 55
  • 70