无法在通知中添加额外的意图
我正在从具有额外整数意图的服务创建通知。出于某种原因,这并不像看起来那么容易。这就是我正在做的事情:
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()
方法读取值时,我得到不一致的结果。有时我会得到我在之前的通知中设置的值。例如,当我将额外值设置为 4 时,我得到的值是 3。我知道这非常令人困惑和奇怪,这就是为什么我想知道是否有人经历过这样的事情以及您可能已经采取了哪些措施来修复它。谢谢!
I'm creating a notification from a Service that has an intent with an extra integer. For some reason, this isn't as easy as it seems. This is what I'm doing:
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);
And then from SomeClass, it reads like this...
if(getIntent().getExtras() != null)
load(getIntent().getExtras().getInt("load", -1));
What's weird is that I get inconsistent results when I read the value from the getInt()
method. Sometimes I get values of things I set in a previous notifications. For example, I get a value of 3 when I had set that extra to be 4. I know this is very confusing and strange, which is why I'm wondering if anyone has experienced anything like this and what you may have done to fix it. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该尝试使用
PendingIntent.FLAG_UPDATE_CURRENT
当您调用PendingIntent.getActivity
时。从文档中:我自己遇到了这个问题,并从 这个答案。
You should try using
PendingIntent.FLAG_UPDATE_CURRENT
when you callPendingIntent.getActivity
. From the documentation:I ran into this issue myself, and found the answer from a comment on this answer.