Notification Auto-Cancel does not call DeleteIntent

寵の児 提交于 2020-01-02 01:12:06

问题


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?


回答1:


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)



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/13078230/notification-auto-cancel-does-not-call-deleteintent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!