0

Voici donc le code que j'utilise pour mes intentions:deuxième bouton de notification, le faisceau que je reçois dans IntentService contient les extras du premier bouton

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

Voilà comment je crée les 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 appelle action2PendingIntent et action appelle action1PendingIntent. Mais dans mon intentionService. J'ai dans mon 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); 
} 

mais le bundle.getString ("tag"); toujours revenir « #Business » (l'étiquette de la première action, même si j'appuie sur le deuxième bouton. Pourquoi est-ce qui se passe?

+1

utiliser différentes requestCodes lors de l'appel .getService() – pskink

Répondre

0

Merci à @pskink j'ai réussi à le faire fonctionner, en modifiant le code de demande pour la deuxième intention dans l'attente, comme ceci:

PendingIntent action2PendingIntent = PendingIntent.getService(context, 2, 
     action2Intent, PendingIntent.FLAG_ONE_SHOT); 
1

Utilisez le PendingIntent.FLAG_UPDATE_CURRENT ci-dessus mentionnés drapeaux aide à mettre à jour l'intention dans l'attente de l'activité.