无法在通知中添加额外内容

Bri*_*ian 3 service notifications android android-intent extra

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

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

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

if(getIntent().getExtras() != null)
    load(getIntent().getExtras().getInt("load", -1));
Run Code Online (Sandbox Code Playgroud)

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

Jak*_*ile 5

你应该PendingIntent.FLAG_UPDATE_CURRENT在打电话时尝试使用PendingIntent.getActivity.从文档:

如果您要创建只有附加内容更改的意图,并且不关心接收到您之前的PendingIntent的任何实体将能够使用您的新附加功能启动它,即使它们未明确赋予它,也可以使用此功能.

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