13

I'm implementing GCM in my app and keeping a hash of notifications to keep track of what is in the notification shade (I have to change intents based on if the user is in or out of app).

I set the deleteIntent PendingIntent for all my notifications. All this does is remove the Notification from my local hash so it won't be updated anymore. The intent is fired fine if I clear all or swipe to delete a notification. However, I also set my notifications to auto cancel. Clicking on a notification does not trigger the deleteIntent for my notification.

My question is, is there any way to be notified when my Notifications are auto-cancelled?

Ben Harris
  • 5,664
  • 3
  • 30
  • 27
  • I observe the same behavior in my application. I'm not sure if this behavior depends on certain phone models. I use Samsung Galaxy Ace for testing, and indeed auto cancel does not fire deleteIntent. – Michael P Nov 29 '12 at 12:26

3 Answers3

21

This bug has been reported, but it doesn't look like it has been investigated at all. To work around this here's what I did:

  • Turn off auto cancel
  • Use broadcast for both content and delete intents with different actions
  • Broadcast receiver checks action
    • Content action: Do both click and delete operations, and cancel notification manually
    • Delete action: Do delete operation only

For example:

Send Notification

Notification.Builder builder = new Notification.Builder(context)
    // Set other properties (not auto-cancel)
    .setContentIntent(PendingIntent.getBroadcast(context, 0, new Intent(NOTIFICATION_CLICKED_ACTION), 0))
    .setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(NOTIFICATION_DELETED_ACTION), 0));
notificationManager.notify(NOTIFICATION_ID, builder.build());

Receive Broadcast

if (intent.getAction().equals(NOTIFICATION_CLICKED_ACTION)) {
    startActivity(new Intent(context, MyActivity.class));
    notificationManager.cancel(NOTIFICATION_ID);
}
// Do deletion behaviour here (for both click and delete actions)
svattom
  • 320
  • 3
  • 12
  • 2
    Newbie here. Where do I write "Receive Broadcast" code? – wakeup Oct 30 '15 at 08:15
  • @NehaK Normally this would be in a Service that runs in the background. – svattom Mar 02 '17 at 21:33
  • Future readers, you can get `notificationManager` by doing `NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);` – olfek Jun 23 '18 at 19:13
  • Just want to point out for people who might come across this solution that it will not work for applications targeting Android 12 as Google has added a restriction to what they call notification trampolines: https://developer.android.com/about/versions/12/behavior-changes-12#notification-trampolines – Benjamin M. Feb 25 '22 at 11:08
1

Documentation says here and here, that clicking on notification with FLAG_AUTO_CANCEL cancels it automatically. This behavior means also that regular contentIntent (if set) will fire along with automatic cancellation, because it is fired in response for user's click action. Use contentIntent field along with deleteIntent to detect cancellation performed by explicit user tap.

Michael P
  • 670
  • 7
  • 23
1

This is the correct behaviour od the DeleteIntent as described here in the Android SDK documentation:

Supply a PendingIntent to send when the notification is cleared explicitly by the user.

The DeleteIntent will only get called when the notification is explicitly cleared by the user by swiping it away or by using the "clear all" function of the notification menu. Tapping on the notification will ONLY trigger the ContentIntent EVEN IF the AutoCancel is set to True.

Erwan
  • 3,733
  • 30
  • 25
  • 13
    Sorry, downvoting as [documentation describes here](http://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)). **The PendingIntent set with setDeleteIntent(PendingIntent) will be sent when this happens** – Ilja S. Mar 27 '14 at 20:08
  • 1
    @IljaS. you are saying the same thing as me: pending intent set with setDeleteIntent is called when the notification is clear. – Erwan Mar 31 '14 at 05:26
  • 5
    I don't feel that touching a notification is the same as it being "cleared explicitly by the user". The intentions of the two actions are not the same. When the user intends to interact with the app, they will touch it. When the user intends to ignore the app, they will clear/dismiss it. They should not be treated the same. – dm78 Dec 04 '14 at 16:20