【问题标题】:Cancelled Notification being redelivered when other notification is touched. Android当其他通知被触摸时,取消通知正在重新传递。安卓
【发布时间】:2014-08-18 22:45:51
【问题描述】:

我有一个问题,无论用户触摸什么通知,我总是收到相同的通知,即使它被取消了。

用户触摸的第一个通知是唯一会传递给我的待定意图的通知,直到设备重新启动(然后第一个触摸的通知成为新的加利福尼亚酒店通知)。

这是我构建通知的方式:

    final Bundle extras = new Bundle();
    final int notificationId = Integer.parseInt(payload.getObjectId());

    extras.putInt(NOTIFICATION_ID, notificationId);

    final Intent intent = new Intent(context,
            PushNotificationLauncherActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    intent.putExtras(extras);

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
    builder.setContentTitle(context.getString(R.string.app_name));
    builder.setContentText(payload.getText());
    builder.setSmallIcon(R.drawable.icon_launcher);
    builder.setAutoCancel(true);

    final PendingIntent pIntent = PendingIntent.getActivity(context, 0,
            intent, 0, extras);

    builder.setContentIntent(pIntent);

    final Notification notification;
    if (Build.VERSION.SDK_INT < 16) {
        notification = builder.getNotification();
    } else {
        notification = builder.build();
    }

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(notificationId, notification);

然后我的 PushNotificationLauncherActivity 中有这个:

    final Bundle extras = getIntent().getExtras();

    final int notificationId = extras
            .getInt(SocialcastGoogleCloudMessageReceiver.NOTIFICATION_ID);

    final NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(NOTIFICATION_SERVICE);
    // please go away
    notificationManager.cancel(notificationId);

    Log.d(PushNotificationLauncherActivity.class.getSimpleName(),
            "Touched notification ID: " + String.valueOf(notificationId));

无论我触摸哪个通知,日志中都会始终显示Touched notification ID: 1234,即使我触摸的通知的ID 为56789

我非常怀疑这是我的测试设备的问题,但如果它有帮助;我正在使用带有 KitKat 4.4.4 的 2013 Nexus 7 和 Dalvik 运行时(不是 ART)构建 KTU84P。

【问题讨论】:

  • 这与此不同:stackoverflow.com/questions/3479428/… 在这方面,我的通知确实消失了。问题是当添加新通知时,接收者会再次收到第一个僵尸通知。 #notadupe

标签: java android notifications android-notifications notificationmanager


【解决方案1】:

在此处将您的notificationId 作为request code 传递:

final PendingIntent pIntent = PendingIntent.getActivity(context, 0,
        intent, 0, extras);

将其更改为:

final PendingIntent pIntent = PendingIntent.getActivity(context, notificationId,
        intent, 0, extras);

比较两个Intent 实例时,不比较包含的Bundles。所以,从android的角度来看,这两个意图是相同的。

有趣的是,reqCodePendingIntent.getActivity(...) 中的第二个参数)比较的。这应该使您的 Intents 独一无二。

【讨论】:

  • 有趣,我会试试的。谢谢!
  • @BlakeBarrett 啊,是的。该文档在此问题上确实具有误导性。我很高兴这有帮助。我记得自己很难弄清楚这一点。
猜你喜欢
  • 2011-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-19
相关资源
最近更新 更多