How to pass extra data to a broadcast receiver with android remote input

1.2k views Asked by At

I've seen a couple of duplicates for this question so I'm sorry in advance if I've missed one but none that I've found have a solution, case in point here I'm trying to do the same thing as this though. I want to use direct reply (Messaging style) notifications but I need to add some extra data (recipientId etc) and I'm confused as to where I can add this extra data my code for the intent, pending intent, and RemoteInput looks like this

    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();

now to send that extra data I have tried adding it as strings

intent.putExtra(Constants.MSG_RECIPIENT, message.getRecipientId());

and as bundles

Bundle bundle = new Bundle();
bundle.putString(Constants.MSG_RECIPIENT, message.getRecipientId());
intent.putExtras(bundle);

in both the intent and as part of the RemoteInput

RemoteInput remoteInput = new RemoteInput.Builder(NOTIFICATION_REPLY)
.setLabel(replyLabel).addExtras(bundle).build();

and in trying to get the data back I've tried to get it from the onReceive intent as either a string or a bundle

intent.getExtras(); 
intent.getStringExtra(Constants.MSG_RECIPIENT);

but nothing seems to work for me the results are always null on the other side (data is definitely not null when sent) can somebody tell me how to add the intent data and retrieve it on the other side?

UPDATE

Ok it now works I'm going to add the working code for others so to send the data i'm adding it to the first intent and calling FLAG_UPDATE_CURRENT on the pending intent

    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();

and to receive it I'm using this in onReceive

 String senderId = intent.getStringExtra(Constants.MSG_SENDER_NAME);
1

There are 1 answers

0
David Wasser On BEST ANSWER

Try adding PendingIntent.FLAG_UPDATE_CURRENT to getBroadcast():

replyPendingIntent = 
        PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

Also, you didn't show where you add the "extras" to the Intent. You must add them BEFORE you call PendingIntent.getBroadcast().