如何在Android远程输入中向广播接收器传递额外数据

3

我看到这个问题有几个重复的,所以如果我错过了一个,我很抱歉,但没有一个我找到的有解决方案,例如这里。 我也想做同样的事情。我想使用直接回复(Messaging style)通知,但我需要添加一些额外的数据(如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);

1
尝试在 getBroadcast() 中添加 PendingIntent.FLAG_UPDATE_CURRENTreplyPendingIntent = PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);另外,您没有展示在 Intent 中添加“extras”的位置。您必须在调用 PendingIntent.getBroadcast() 之前添加它们。 - David Wasser
1
非常感谢,这对我有用。我把额外的东西放在了正确的位置,所以我猜是FLAG_UPDATE_CURRENT修复了它。如果您想将其作为答案添加,我很乐意接受。 - martinseal1987
1个回答

4
尝试在 getBroadcast() 中添加 PendingIntent.FLAG_UPDATE_CURRENT
replyPendingIntent = 
        PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

此外,您没有展示您在Intent中添加“额外信息”的位置。您必须在调用PendingIntent.getBroadcast()之前添加它们。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接