通知中的第二个按钮,我在 IntentService 中收到的包包含第一个按钮的额外内容
Second button from notification, the bundle I receive in IntentService contains the extras from the first button
所以这是我用于我的意图的代码:
String tag = "#business"; String secondTag = "#personal";
Intent action1Intent = new Intent(context, NotificationActionService.class)
.setAction("tag").putExtra("id", psTrip.getId()).putExtra("tag", tag);
PendingIntent action1PendingIntent = PendingIntent.getService(context, 0,
action1Intent, PendingIntent.FLAG_ONE_SHOT);
Intent action2Intent = new Intent(context, NotificationActionService.class)
.setAction("tag").putExtra("id", psTrip.getId()).putExtra("tag", secondTag);
PendingIntent action2PendingIntent = PendingIntent.getService(context, 0,
action2Intent, PendingIntent.FLAG_ONE_SHOT);
这是我创建动作的方式:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
RemoteInput remoteInput = new RemoteInput.Builder("Business")
.setLabel("Business")
.build();
Notification.Action action =
new Notification.Action.Builder(R.drawable.btn_tag_trip,
tag, action1PendingIntent)
.addRemoteInput(remoteInput)
.build();
RemoteInput remoteInput2 = new RemoteInput.Builder("Personal")
.setLabel("Personal")
.build();
Notification.Action action2 =
new Notification.Action.Builder(R.drawable.btn_tag_trip,
secondTag, action2PendingIntent)
.addRemoteInput(remoteInput2)
.build();
noti = new Notification.Builder(context)
.setContentTitle(context.getString(R.string.passenger_name))
.setContentText(content).setSmallIcon(R.drawable.notification_icon)
.setContentIntent(pIntent)
.addAction(action)
.addAction(action2).build();
}
Action2 调用 action2PendingIntent,action 调用 action1PendingIntent。
但在我的意图服务。我的 onHandleIntent 中有这个:
final NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();
String action = intent.getAction();
Log.i("", "Received notification action: " + action);
Bundle bundle = intent.getExtras();
if (bundle != null) {
final String id = bundle.getString("id");
final String tag = bundle.getString("tag");
Log.i("", "Received notification action: id: " + id + " / tag: " + tag);
}
但是 bundle.getString("tag");将始终 return“#business”(第一个动作的标签,即使我按下第二个按钮。
为什么会这样?
使用上述 PendingIntent.FLAG_UPDATE_CURRENT 标志有助于更新 activity 的未决意图。
感谢@pskink,我通过更改第二个未决意图的请求代码,设法让它工作,如下所示:
PendingIntent action2PendingIntent = PendingIntent.getService(context, 2,
action2Intent, PendingIntent.FLAG_ONE_SHOT);
所以这是我用于我的意图的代码:
String tag = "#business"; String secondTag = "#personal";
Intent action1Intent = new Intent(context, NotificationActionService.class)
.setAction("tag").putExtra("id", psTrip.getId()).putExtra("tag", tag);
PendingIntent action1PendingIntent = PendingIntent.getService(context, 0,
action1Intent, PendingIntent.FLAG_ONE_SHOT);
Intent action2Intent = new Intent(context, NotificationActionService.class)
.setAction("tag").putExtra("id", psTrip.getId()).putExtra("tag", secondTag);
PendingIntent action2PendingIntent = PendingIntent.getService(context, 0,
action2Intent, PendingIntent.FLAG_ONE_SHOT);
这是我创建动作的方式:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
RemoteInput remoteInput = new RemoteInput.Builder("Business")
.setLabel("Business")
.build();
Notification.Action action =
new Notification.Action.Builder(R.drawable.btn_tag_trip,
tag, action1PendingIntent)
.addRemoteInput(remoteInput)
.build();
RemoteInput remoteInput2 = new RemoteInput.Builder("Personal")
.setLabel("Personal")
.build();
Notification.Action action2 =
new Notification.Action.Builder(R.drawable.btn_tag_trip,
secondTag, action2PendingIntent)
.addRemoteInput(remoteInput2)
.build();
noti = new Notification.Builder(context)
.setContentTitle(context.getString(R.string.passenger_name))
.setContentText(content).setSmallIcon(R.drawable.notification_icon)
.setContentIntent(pIntent)
.addAction(action)
.addAction(action2).build();
}
Action2 调用 action2PendingIntent,action 调用 action1PendingIntent。 但在我的意图服务。我的 onHandleIntent 中有这个:
final NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();
String action = intent.getAction();
Log.i("", "Received notification action: " + action);
Bundle bundle = intent.getExtras();
if (bundle != null) {
final String id = bundle.getString("id");
final String tag = bundle.getString("tag");
Log.i("", "Received notification action: id: " + id + " / tag: " + tag);
}
但是 bundle.getString("tag");将始终 return“#business”(第一个动作的标签,即使我按下第二个按钮。 为什么会这样?
使用上述 PendingIntent.FLAG_UPDATE_CURRENT 标志有助于更新 activity 的未决意图。
感谢@pskink,我通过更改第二个未决意图的请求代码,设法让它工作,如下所示:
PendingIntent action2PendingIntent = PendingIntent.getService(context, 2,
action2Intent, PendingIntent.FLAG_ONE_SHOT);