android - 通知通过旧的 Intent Extras

标签 android android-notifications android-pendingintent android-notification-bar

我正在通过此代码在 BroadcastReceiver 中创建通知:

String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
        int icon = R.drawable.ic_stat_notification;
        CharSequence tickerText = "New Notification";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        long[] vibrate = {0,100,200,200,200,200};
        notification.vibrate = vibrate;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        CharSequence contentTitle = "Title";
        CharSequence contentText = "Text";
        Intent notificationIntent = new Intent(context, NotificationActivity.class);
        notificationIntent.putExtra(Global.INTENT_EXTRA_FOO_ID, foo_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        int mynotification_id = 1;

        mNotificationManager.notify(mynotification_id, notification);

当我点击通知时,它会打开 NotificationActivity 并在 Activity 内我可以从 Intent-Bundle 中检索 foo_id(例如 1)

但是,如果触发了另一个通知并且我再次单击它,则 Activity 仍会从 Intent-Bundle 接收“旧”值 (1)。我尝试使用 clear() 清除 bundle ,但效果相同。我认为我的代码有问题..

最佳答案

您正在为待处理的 Intens 发送相同的请求代码。 改变这个:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

收件人:

PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);

如果您发送相同的参数,则不会创建 Intent 。它们被重复使用。

关于android - 通知通过旧的 Intent Extras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7370324/

相关文章:

android - 使用按钮创建 EditText

Android Studio Gradle 报错 Error :Cause: net. ruby​​grapefruit.platform.internal.jni.PosixProcessFunctions.getPid()I

android - 新 Intent 中的 NullPointerException

Android:取消服务中的警报

Android 不会停止 GPS Intent

javascript - 是否可以通过我的应用程序中的反向代理连接 Google Maps API?

android - 实用更新应用程序时出现 PackageInstaller 错误

java - 当应用程序被系统杀死时如何删除通知?

android - Android Wear 通知的意外图标和背景颜色

Android:使用 AlarmManager 设置定期警报