[英]Android Notification Action - Intent Extra not working as expected
I'm creating a notification
with multiple actions.我正在创建包含多项操作的
notification
。 I'm using broadcast intents
to communicate that one has been pushed and take specific action.我正在使用
broadcast intents
来传达一个已被推送并采取具体行动。 There are 4 buttons, and I've created 4 separate intents.有 4 个按钮,我创建了 4 个独立的意图。 Each one has the same Action string, but a different
StringExtra
.每个都有相同的 Action 字符串,但有不同的
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.但无论我推送哪一个,传递给
receiver
的额外内容始终是最后定义的动作。 That is, in the example above, every button returns a String Extra equal to ACT_NEVER
.也就是说,在上面的示例中,每个按钮都返回一个等于
ACT_NEVER
的 String Extra。 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.如果我对
.addAction
重新排序,那么 intLater 是最后一个,无论我按下哪个按钮,接收者都会告诉我 String Extra 等于ACT_LATER
。
I can't figure out why - the 4 Intents
are completely independent of each other.我不明白为什么——这 4 个
Intents
是完全独立的。 The actions specify the correct Intent
.这些操作指定了正确的
Intent
。 What's going on?这是怎么回事? I'm stumped.
我很难过。
PendingIntent.FLAG_UPDATE_CURRENT
,so the last PendingIntent's extra will replace pre one.PendingIntent.FLAG_UPDATE_CURRENT
,因此最后一个 PendingIntent 的额外内容将替换前一个。 So to solve your problem,you just need to set different requestCode
for four PendingIntent
,like this:所以要解决你的问题,你只需要为四个
PendingIntent
设置不同的requestCode
,就像这样:
.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:我已经阅读了有关
addAction()
的文档,其中有一些非常有趣的内容:
You use 0 as a Icon, and 4 actions, maybe this have some impact on behavior您使用 0 作为图标,并使用 4 个动作,这可能会对行为产生一些影响
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.