Second button from notification, the bundle I receive in IntentService contains the extras from the first button

104 views Asked by At

So this is the code I use for my intents:

  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);

This is how I create the actions:

   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 calls action2PendingIntent and action calls action1PendingIntent. But in my intentService. I have this in my 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);
}

BUT the bundle.getString("tag"); will always return "#business" (the tag of the first action, even if I press the second button. Why is this happening?

2

There are 2 answers

0
rosu alin On BEST ANSWER

Thanks to @pskink I did manage to make it work, by changing the request code for the second pending intent, like this:

  PendingIntent action2PendingIntent = PendingIntent.getService(context, 2,
        action2Intent, PendingIntent.FLAG_ONE_SHOT);
0
K Sathish On

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