gpt4 book ai didi

android - Notification onClick Intent 向 Activity 发送错误数据

转载 作者:搜寻专家 更新时间:2023-11-01 07:47:35 25 4
gpt4 key购买 nike

当我的应用程序中的通知被点击时,我正在使用 newIntent 传递正确的 taskId。这是我的广播接收器的 onReceive() 中的代码:

 Intent newIntent = new Intent(context, TaskActivity.class);
int taskId = intent.getIntExtra("taskId", 0);
newIntent.putExtra("taskId", intent.getIntExtra("taskId", 0));

Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(intent.getStringExtra("NotificationTitle"))
.setContentText(intent.getStringExtra("NotificationText"))
.setSmallIcon(intent.getIntExtra("NotificationIcon", 0))
.setContentIntent(PendingIntent.getActivity(context, 0, newIntent, 0))
.build();

这是接收 Intent 的 Activity 代码

if (getIntent().hasExtra("taskId")) {
currentTask = dataSource.fetchTask(getIntent().getIntExtra("taskId", 0));
}

当我调试时,方法 getIntExtra() 返回的值与广播接收器的 onReceive() 中的值不同。

知道为什么会这样吗?

谢谢!

最佳答案

当您创建 Notification 时,而不是:

.setContentIntent(PendingIntent.getActivity(context, 0, newIntent, 0))

这样做:

.setContentIntent(PendingIntent.getActivity(context, 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT))

您的问题是您多次重复使用相同的 PendingIntent。您需要确保每次使用时更新 PendingIntent 中的“extras”。

注意:如果您同时有多个可用的 Notification,则需要确保为每个创建唯一的 PendingIntent。为此,请确保 requestCode 参数(PendingIntent.getActivity() 中的第二个参数)对于每个 Notification 都是不同的(您可以,例如,将您的 taskId 用作唯一的 requestCode

关于android - Notification onClick Intent 向 Activity 发送错误数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40754505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com