我已经看到几个重复的问题,所以如果您错过了一个问题,但对于找不到的解决方案,我在此表示歉意,here做同样的事情。我想使用直接回复(消息传递样式)通知,但是我需要添加一些额外的数据(recipientId等),而我在哪里可以将这些额外的数据添加到我的意图,待定意图和RemoteInput的代码中,我感到困惑这个
String replyLabel = getString(R.string.notif_action_reply);
intent = new Intent(this, ReplyReceiver.class);
replyPendingIntent = PendingIntent.getBroadcast(this,0,intent,0);
RemoteInput remoteInput = new
RemoteInput.Builder(NOTIFICATION_REPLY).setLabel(replyLabel).build();
NotificationCompat.Action action = new
NotificationCompat.Action.Builder(R.drawable.ic_send_white_24dp,
this.getString(R.string.reply), replyPendingIntent)
.addRemoteInput(remoteInput)
.setAllowGeneratedReplies(true)
.build();
现在要发送多余的数据,我已经尝试将其添加为字符串
intent.putExtra(Constants.MSG_RECIPIENT, message.getRecipientId());
并作为捆绑包
Bundle bundle = new Bundle();
bundle.putString(Constants.MSG_RECIPIENT, message.getRecipientId());
intent.putExtras(bundle);
在意图中和作为RemoteInput的一部分
RemoteInput remoteInput = new RemoteInput.Builder(NOTIFICATION_REPLY)
.setLabel(replyLabel).addExtras(bundle).build();
,在尝试取回数据时,我尝试从onReceive意图以字符串或束的形式获取数据
intent.getExtras();
intent.getStringExtra(Constants.MSG_RECIPIENT);
但是对我来说似乎没有任何作用,结果在另一侧始终为空(发送时数据绝对不为空)有人可以告诉我如何添加意图数据并在另一侧进行检索吗?
更新
好了,现在可以正常工作了,我将为其他人添加工作代码,以便发送数据,然后将其添加到第一个意图,并在未决意图上调用FLAG_UPDATE_CURRENT
String replyLabel = getString(R.string.notif_action_reply);
intent = new Intent(this, ReplyReceiver.class);
//intent.putExtras(bundle);
intent.putExtra(Constants.MSG_SENDER_NAME, message.getSenderName());
intent.putExtra(Constants.MSG_SENDER, message.getSenderId());
intent.putExtra(Constants.MSG_RECIPIENT_NAME,
message.getRecipientName());
intent.putExtra(Constants.MSG_RECIPIENT, message.getRecipientId());
replyPendingIntent = PendingIntent.getBroadcast
(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
RemoteInput remoteInput = new
RemoteInput.Builder(NOTIFICATION_REPLY).setLabel(replyLabel).build();
NotificationCompat.Action action = new
NotificationCompat.Action.Builder(R.drawable.ic_send_white_24dp,
this.getString(R.string.reply), replyPendingIntent)
.addRemoteInput(remoteInput)
.setAllowGeneratedReplies(true)
.build();
要接收它,我在onReceive中使用它
String senderId = intent.getStringExtra(Constants.MSG_SENDER_NAME);
答案 0 :(得分:0)
尝试将PendingIntent.FLAG_UPDATE_CURRENT
添加到getBroadcast()
:
replyPendingIntent =
PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
此外,您也没有显示将“附加”添加到Intent的位置。您必须在致电PendingIntent.getBroadcast()
之前添加它们。