RemoteInput return null

466 views Asked by At

I am creating a notification with RemoteInput to capture the text from the notification itself.

The problem that when I retrieve the RemoteInput in the called service with RemoteInput.getResultsFromIntent() it returns null. I have verified that the code is as indicated in the google api. What can happen?

Activity:

Intent intentMenu = new Intent(getApplicationContext(), NotificationToolsService.class);
    intentMenu.setAction("personalACTION");

    RemoteInput remoteInput = new RemoteInput.Builder(FV.REMOTE_INPUT_KEY)
            .setLabel("Message...").build();

    PendingIntent pIntentMenu = PendingIntent.getService(getApplicationContext(),20,intentMenu,PendingIntent.FLAG_IMMUTABLE);

    NotificationCompat.Action remoteInputAction = new NotificationCompat.Action.Builder(0,"Reply",pIntentMenu)
            .addRemoteInput(remoteInput)
            .build();

    Notification notificationRemoteInput = new NotificationCompat.Builder(getApplicationContext(),FV.CHANNEL_TEST)
            .setSmallIcon(R.drawable.linterna)
            .setContentTitle("Conversation with Javier")
            .setContentText("Javier: Hi! How are you?")
            .addAction(remoteInputAction)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.foto1)).build();

    notificationManagerCompat.notify(FV.NOTIFICATION_CONVERSATION,notificationRemoteInput);

Service:

public int onStartCommand(Intent intent, int flags, int startId) {

    if(intent.getAction() == "personalACTION") {
        Bundle keys = RemoteInput.getResultsFromIntent(intent);
        CharSequence text = keys.getCharSequence(FV.REMOTE_INPUT_KEY);
        Toast.makeText(this, "Reply with"+text, Toast.LENGTH_SHORT).show();

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        Notification notificationRemoteInput = new NotificationCompat.Builder(this,FV.CHANNEL_TEST)
                .setSmallIcon(R.drawable.linterna)
                .setContentTitle("Conversation with Javier")
                .setContentText("Javier: Hi! How are you? \n Me:"+text)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.foto1)).build();

        notificationManager.notify(FV.NOTIFICATION_CONVERSATION,notificationRemoteInput);
    }

    return super.onStartCommand(intent, flags, startId);
}
2

There are 2 answers

1
programmer dreamer On

In addition to @Nesyou's answer, since Android S (SDK 31), it is a MUST to include a mutability flag to your pending intent. Hence, you can do something like this:

PendingIntent replyPendingIntent;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
        replyPendingIntent = PendingIntent.getBroadcast(context,
                notificationId,
                getMessageReplyIntent(context, notificationId, senderId),
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
    } else {
        replyPendingIntent = PendingIntent.getBroadcast(context,
                notificationId,
                getMessageReplyIntent(context, notificationId, senderId),
                PendingIntent.FLAG_UPDATE_CURRENT);
    }
0
Unes On

In the PendingIntent use PendingIntent.FLAG_UPDATE_CURRENT instead of PendingIntent.FLAG_IMMUTABLE. An error will appear just ignore it.