An alternative to the deleteIntent
is the following which has proved beneficial in my own app:
Basically, you create an intent with your notification that starts an IntentService (or any other service) and in onHandleIntent
you can set a flag indicating whether the notification is active.
You can set this intent to be fired when the user taps the notification (contentIntent) and/or when the user clears it from the list (deleteIntent).
To illustrate it, here is what I do in my own app. When building the notification I set
Intent intent = new Intent(this, CleanupIntentService.class);
Notification n = NotificationCompat.Builder(context).setContentIntent(
PendingIntent.getActivity(this, 0, intent, 0)).build();
When the notification is tapped, my CleanupIntentService
is launched, setting a flag (in the service that created the notification):
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onCreate(); // If removed, onHandleIntent is not called
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
OtherService.setNotificationFlag(false);
}