0

所以这是我用于我的意图的代码:

  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"); 将始终返回“#business”(第一个操作的标记,即使我按下第二个按钮。为什么会这样?

4

2 回答 2

1

Use the PendingIntent.FLAG_UPDATE_CURRENT above mentioned flags helps in updating pending intent for the activity.

于 2017-10-10T12:20:08.833 回答
0

感谢@pskink,我确实设法通过更改第二个待处理意图的请求代码来使其工作,如下所示:

  PendingIntent action2PendingIntent = PendingIntent.getService(context, 2,
        action2Intent, PendingIntent.FLAG_ONE_SHOT);
于 2017-10-10T12:30:47.030 回答