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.
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))
I have read documentation about addAction()
, and quite interesting stuff is there:
You use 0 as a Icon, and 4 actions, maybe this have some impact on behavior
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With