Determine addAction click for Android notifications

It’s because you’re using FLAG_UPDATE_CURRENT with Intents that have the same action

From the docs:

if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent.

When you specify pendingIntentMaybe and pendingIntentNo, the system uses the PendingIntent created for pendingIntentYes, but it overwrites the extras. Thus, all three variables refer to the same object, and the last extras specified were for pendingIntentNo.

You should specify an alternative action for each Intent. You can still have one BroadcastReceiver, and just have it intercept all three actions. This would be less confusing semantically as well 🙂

Your Notification poster:

//Yes intent
Intent yesReceive = new Intent();  
yesReceive.setAction(YES_ACTION);
PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes);

//Maybe intent
Intent maybeReceive = new Intent();  
maybeReceive.setAction(MAYBE_ACTION);
PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe);

//No intent
Intent noReceive = new Intent();  
noReceive.setAction(NO_ACTION);
PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo);

Your receiver:

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if(YES_ACTION.equals(action)) {
        Log.v("shuffTest","Pressed YES");
    } else if(MAYBE_ACTION.equals(action)) {
        Log.v("shuffTest","Pressed NO");
    } else if(NO_ACTION.equals(action)) {
        Log.v("shuffTest","Pressed MAYBE");
    }
}           

Leave a Comment