android - 不能在通知中为 Intent 添加额外内容

标签 android service notifications android-intent extra

我正在从具有额外整数 Intent 的服务创建通知。出于某种原因,这并不像看起来那么容易。这就是我正在做的:

Notification notification = new Notification(R.drawable.notification_icon, "Title", 0);
Intent relaunchIntent = new Intent(this, SomeClass.class);
relaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
relaunchIntent.putExtra("load", getId());
notification.setLatestEventInfo(this, "Title", "Tap here to open", PendingIntent.getActivity(this, 0, relaunchIntent, 0);
notificationManager.notify(1234, notification);

然后从 SomeClass 中,它看起来像这样......

if(getIntent().getExtras() != null)
    load(getIntent().getExtras().getInt("load", -1));

奇怪的是,当我从 getInt() 方法读取值时,得到的结果不一致。有时我会得到我在之前通知中设置的值。例如,当我将 extra 设置为 4 时,我得到的值为 3。我知道这非常令人困惑和奇怪,这就是为什么我想知道是否有人经历过这样的事情以及你可能做了什么来修复它。谢谢!

最佳答案

您应该尝试使用 PendingIntent.FLAG_UPDATE_CURRENT当您调用 PendingIntent.getActivity 时。来自文档:

This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

我自己遇到了这个问题,并从 this answer 的评论中找到了答案.

关于android - 不能在通知中为 Intent 添加额外内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7098893/

相关文章:

java - Android Studio : Layout for phone and tablet

javascript - 多个 IntenseDebate 评论计数

android - 辅助功能服务未在服务中列出?

android - 预定的本地通知不会出现在 flutter 中

ios - 当应用程序运行且通知托盘打开时处理推送通知

java - TFL JSON数据解析

android - 联系 API 时出错,com.google.android.gms.tasks.RuntimeExecutionException :com. google.android.gms.api.ApiException:13:错误

android - PBKDF2 与 Android 上的 SHA256

bash - Ubuntu 在前台运行服务

objective-c - 如何让 NSUserNotification 超时?