Search code examples
androidandroid-intentandroid-notifications

Android Notification Action - Intent Extra not working as expected


I'm creating a notification with multiple actions. I'm using broadcast intents to communicate that one has been pushed and take specific action. There are 4 buttons, and I've created 4 separate intents. Each one has the same Action string, but a different StringExtra.

Intent intNow = new Intent(mThis, MyReceiver.class).setAction(actionNotify).putExtra("button", ACT_NOW);
    Intent intEmail = new Intent(mThis, MyReceiver.class).setAction(actionNotify).putExtra("button", ACT_EMAIL);
    Intent intLater = new Intent(mThis, MyReceiver.class).setAction(actionNotify).putExtra("button", ACT_LATER);
    Intent intNever = new Intent(mThis, MyReceiver.class).setAction(actionNotify).putExtra("button", ACT_NEVER);

    Notification.Builder myRatingNotification = new Notification.Builder(mThis)
            .setContentTitle(title)
            .setContentText(text)
            .setSmallIcon(R.mipmap.ic_launcher)
            .addAction(0, mThis.getString(R.string.Rate_Act_Now), PendingIntent.getBroadcast(mThis, 0, intNow, PendingIntent.FLAG_UPDATE_CURRENT))
            .addAction(0, mThis.getString(R.string.Rate_App_Email), PendingIntent.getBroadcast(mThis, 0, intEmail, PendingIntent.FLAG_UPDATE_CURRENT))
            .addAction(0, mThis.getString(R.string.Rate_Act_Later), PendingIntent.getBroadcast(mThis, 0, intLater, PendingIntent.FLAG_UPDATE_CURRENT))
            .addAction(0, mThis.getString(R.string.Rate_Act_Never), PendingIntent.getBroadcast(mThis, 0, intNever, PendingIntent.FLAG_UPDATE_CURRENT))
            .setAutoCancel(true);

    Notification notification = new Notification.BigTextStyle(myRatingNotification).bigText(text).build();
    ((NotificationManager) mThis.getSystemService(Context.NOTIFICATION_SERVICE)).notify(notificationId, notification);

So the notification is created successfully. The buttons are there. But no matter which one I push, the extra that is passed to the receiver is always the last action defined. That is, in the example above, every button returns a String Extra equal to ACT_NEVER. If I reorder the .addAction so intLater is last, the receiver tells me that the String Extra is equal to ACT_LATER, no matter which button I push.

I can't figure out why - the 4 Intents are completely independent of each other. The actions specify the correct Intent. What's going on? I'm stumped.


Solution

    1. you should set an icon as first parameter,not 0.
    2. your current result because you use same action and same requestCode to construct a PendingIntent,so the 4 PendingIntent will be the same,and you use PendingIntent.FLAG_UPDATE_CURRENT,so the last PendingIntent's extra will replace pre one.

    So to solve your problem,you just need to set different requestCode for four PendingIntent,like this:

    .addAction(0, mThis.getString(R.string.Rate_Act_Now), PendingIntent.getBroadcast(mThis, 0, intNow, PendingIntent.FLAG_UPDATE_CURRENT))
    .addAction(0, mThis.getString(R.string.Rate_App_Email), PendingIntent.getBroadcast(mThis, 1, intEmail, PendingIntent.FLAG_UPDATE_CURRENT))
    .addAction(0, mThis.getString(R.string.Rate_Act_Later), PendingIntent.getBroadcast(mThis, 2, intLater, PendingIntent.FLAG_UPDATE_CURRENT))
    .addAction(0, mThis.getString(R.string.Rate_Act_Never), PendingIntent.getBroadcast(mThis, 3, intNever, PendingIntent.FLAG_UPDATE_CURRENT))