Search code examples
javaandroidandroid-intentandroid-notificationsandroid-pendingintent

Android notification : bug when using multiple setOnClickPendingIntent


I have got a strange bug.

I have got a notification with 3 buttons (back, next, play/pause). I want to set a different pendingIntent on each of these three buttons which launch a service. Here is my code:

    Intent playPauseIntent = new Intent(MyApp.mainActivity, NotificationService.class);
    playPauseIntent.putExtra("do_action", "play");
    contentView.setOnClickPendingIntent(R.id.notifPlayPauseImageView, PendingIntent.getService(MyApp.mainActivity, 0, playPauseIntent, PendingIntent.FLAG_UPDATE_CURRENT));            

    Intent nextRadioIntent = new Intent(MyApp.mainActivity, NotificationService.class);
    nextRadioIntent.putExtra("do_action", "next");
    contentView.setOnClickPendingIntent(R.id.notifNextImageView, PendingIntent.getService(MyApp.mainActivity, 0, nextRadioIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    Intent previousRadioIntent = new Intent(MyApp.mainActivity, NotificationService.class);
    previousRadioIntent.putExtra("do_action", "previous");
    contentView.setOnClickPendingIntent(R.id.notifPreviousImageView, PendingIntent.getService(MyApp.mainActivity, 0, previousRadioIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    notification.contentView = contentView;
    mNotificationManager.notify(1, notification); 

Then, in my service, I get the intent and the value of "do_action" defined as an extra:

@Override
public int onStartCommand(Intent intent,  int flags, int startId) { 

   if(intent != null && intent.getExtras() != null)
   {
      String action = (String) intent.getExtras().get("do_action"); // BUG HERE ! I always get "previous" !!!
   }       
}

The bug is here... I always get "previous" when I get the "do_action" value (not only when I click on the previous button, also when I click on the play or next buttons).

Anybody can help me to solve this problem ?

Thanks !


Solution

  • You have used same request code 0 for all of your Pendingintent. You should use different REQUEST_CODE for different PendingIntent:

    PendingIntent.getService(MyApp.mainActivity, REQUEST_CODE, Intent, PendingIntent.FLAG_UPDATE_CURRENT)
    

    Try this:

    Intent playPauseIntent = new Intent(MyApp.mainActivity, NotificationService.class);
    playPauseIntent.putExtra("do_action", "play");
    contentView.setOnClickPendingIntent(R.id.notifPlayPauseImageView, PendingIntent.getService(MyApp.mainActivity, 0, playPauseIntent, PendingIntent.FLAG_UPDATE_CURRENT));            
    
    Intent nextRadioIntent = new Intent(MyApp.mainActivity, NotificationService.class);
    nextRadioIntent.putExtra("do_action", "next");
    contentView.setOnClickPendingIntent(R.id.notifNextImageView, PendingIntent.getService(MyApp.mainActivity, 1, nextRadioIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    
    Intent previousRadioIntent = new Intent(MyApp.mainActivity, NotificationService.class);
    previousRadioIntent.putExtra("do_action", "previous");
    contentView.setOnClickPendingIntent(R.id.notifPreviousImageView, PendingIntent.getService(MyApp.mainActivity, 2, previousRadioIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    
    notification.contentView = contentView;
    mNotificationManager.notify(1, notification); 
    

    Hope this will help~