通知自动取消不会调用DeleteIntent

Ben*_*ris 12 android google-cloud-messaging

我正在我的应用程序中实现GCM并保持通知的散列以跟踪通知阴影中的内容(我必须根据用户是否进入应用程序来更改意图).

我为所有通知设置了deleteIntent PendingIntent.所有这一切都是从我的本地哈希中删除通知,因此它将不再更新.如果我清除所有内容或刷卡以删除通知,则意图被解雇.但是,我还将通知设置为自动取消.单击通知不会触发我的通知的deleteIntent.

我的问题是,当我的通知被自动取消时,有什么方法可以得到通知吗?

sva*_*tom 19

已报告此错误,但看起来它根本没有被调查过.解决这个问题就是我做的:

  • 关闭自动取消
  • 对内容使用广播并使用不同的操作删除意图
  • 广播接收器检查动作
    • 内容操作:同时执行单击和删除操作,并手动取消通知
    • 删除操作:仅删除操作

例如:

发送通知

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());
Run Code Online (Sandbox Code Playgroud)

接收广播

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)
Run Code Online (Sandbox Code Playgroud)

  • 新手在这里.我在哪里写"接收广播"代码? (2认同)