简体   繁体   English

Android 通知操作 - Intent Extra 未按预期工作

[英]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.我很难过。

  1. you should set an icon as first parameter,not 0.您应该将图标设置为第一个参数,而不是 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.您当前的结果是因为您使用相同的操作和相同的 requestCode 来构造一个 PendingIntent,所以 4 个 PendingIntent 将是相同的,并且您使用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()的文档,其中有一些非常有趣的内容:

  • A notification in its expanded form can display up to 3 actions展开形式的通知最多可以显示3 个操作
  • Every action must have an icon每个动作都必须有一个图标

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM