Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification Auto-Cancel does not call DeleteIntent

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?

like image 836
Ben Harris Avatar asked Oct 25 '12 22:10

Ben Harris


1 Answers

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)
like image 60
svattom Avatar answered Oct 22 '22 12:10

svattom