18

I'm trying to detect when my notification gets cleared. My question directly refers to this answer which outlines what I'm suppose to do. This is how I'm implementing the actions:

// usual Notification initialization here
notification.deleteIntent = PendingIntent.getService(context, 0, new Intent(context, CleanUpIntent.class), 0);
notificationManager.notify(123, notification)

This is the CleanUpIntent class:

class CleanUpIntent extends IntentService {
    public CleanUpIntent() {
        super("CleanUpIntent");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        // clean up code
    }
}

Afterwards, I simply launch the notification like I normally would but when I go to test it out (pressing "Clear All Notifications") nothing happens. I inserted a line of code that out print something to LogCat when the IntentService gets started, but nothing ever ran. Is this how I'm suppose to use Notification.deleteIntent?

Community
  • 1
  • 1
Brian
  • 7,955
  • 16
  • 66
  • 107

4 Answers4

51

sample code which will be called whenever user clears the notification, hope it will help you .

 ....
 notificationBuilder.setDeleteIntent(getDeleteIntent());
 ....


protected PendingIntent getDeleteIntent()
 {
    Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class);
    intent.setAction("notification_cancelled");
    return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

NotificationBroadcastReceiver.java

@Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if(action.equals("notification_cancelled"))
        {
            // your code
        }
    }

AndroidManifiest.xml

 <receiver android:name=".NotificationBroadcastReceiver">
                <intent-filter>
                    <action android:name="notification_cancelled"/>
                </intent-filter>
            </receiver>
vimal1083
  • 8,499
  • 6
  • 34
  • 50
Pawan Maheshwari
  • 15,088
  • 1
  • 48
  • 50
  • 5
    There is absolutely no need to check `action` equallity – Dmitry Zaytsev Mar 17 '14 at 14:05
  • 8
    An application could have different notifications generated with the same receiver or different actions desired depending on notification content or some other conditions. In those cases, one might want to define an action. – MeanderingCode Oct 03 '14 at 22:52
  • 1
    Be aware that this code reusing the PendingIntent for all notifications this way. Read its description about generating unqiue ones by using different request codes or different data checked by Intent.isEqual(). – althaus May 04 '17 at 09:47
4

What you need to do is register a BroadcastReceiver (probably in your AndroidManifest.xml or alternatively using registerReceiver in a Service) and then set deleteIntent to be an Intent that will be caught by that receiver.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Do you happen to know what the IntentFilter flag is for catching when the notifications are cleared? – Brian Sep 19 '11 at 02:57
  • It isn't an `IntentFilter` flag, it is a `BroadcastReceiver`: see http://code.google.com/p/islamictools/source/browse/trunk/IslamicTools/src/com/alpha/commun/MsgNotification.java?spec=svn11&r=11 for an example of how you would do this. – Femi Sep 19 '11 at 03:07
  • I'm not too familiar with BroadcastReceivers, but my question was regarding how I register the receiver. Don't you need to provide it an IntentFilter whether or not you call Context.registerReceiver or manually put it in the AndroidManifest? – Brian Sep 19 '11 at 03:21
  • 2
    You do. However, if you register the receiver in AndroidManifest.xml you can use `new Intent(context, {ReceiverClassName}.class)` to send an `Intent` to the `BroadcastReceiver` without having to configure an explicit IntentFilter in the AndroidManifest.xml file. If you use `registerReceiver` then you do need to create an IntentFilter – Femi Sep 19 '11 at 03:34
  • @Femi The link on androidcompetencycenter dot com is some kind of noisy trap now. Can you replace or delete the comment? – gnuf Jul 12 '16 at 14:43
  • @Femi google code disabled SVN permanently. So that link is unusable. – nurettin Nov 24 '16 at 17:45
0

You should use getBroadcast methode instead getService and should register receiver for specific Action.

Pankaj
  • 1,242
  • 1
  • 9
  • 21
-4

An explicit receiver is not required. deleteIntent will be called automatically while pressing the clear button.

vimal1083
  • 8,499
  • 6
  • 34
  • 50
basha
  • 1
  • 1