use*_*612 2 notifications android broadcastreceiver android-intent
单击我的应用程序中的通知时,我正在使用 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();
Run Code Online (Sandbox Code Playgroud)
这是接收 Intent 的活动的代码
if (getIntent().hasExtra("taskId")) {
currentTask = dataSource.fetchTask(getIntent().getIntExtra("taskId", 0));
}
Run Code Online (Sandbox Code Playgroud)
当我调试时,方法 getIntExtra() 返回的值与广播接收器的 onReceive() 中的值不同。
任何想法为什么会发生这种情况?
谢谢!
当您创建Notification, 而不是这个:
.setContentIntent(PendingIntent.getActivity(context, 0, newIntent, 0))
Run Code Online (Sandbox Code Playgroud)
做这个:
.setContentIntent(PendingIntent.getActivity(context, 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT))
Run Code Online (Sandbox Code Playgroud)
您的问题是您PendingIntent多次重复使用相同的内容。您需要确保PendingIntent每次使用时都会更新中的“附加内容” 。
注意:如果您同时有多个Notifications 可用,那么您需要确保PendingIntent为每个 s创建唯一的s。为此,请确保每个requestCode参数( 中的第二个参数PendingIntent.getActivity())都不同Notification(例如,您可以将您的taskId用作唯一的requestCode)。
| 归档时间: |
|
| 查看次数: |
732 次 |
| 最近记录: |