【问题标题】:RemoteInput return null远程输入返回空
【发布时间】:2021-09-12 11:28:49
【问题描述】:

我正在使用 RemoteInput 创建一个通知以从通知本身捕获文本。

当我使用 RemoteInput.getResultsFromIntent() 在被调用服务中检索 RemoteInput 时,它返回 null 的问题。我已验证代码如 google api 中所示。会发生什么?

活动:

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

服务:

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

【问题讨论】:

  • 我也有同样的问题。你找到解决办法了吗?

标签: android android-notifications remote-input


【解决方案1】:

在 PendingIntent 中使用 PendingIntent.FLAG_UPDATE_CURRENT 而不是 PendingIntent.FLAG_IMMUTABLE。 会出现错误,忽略即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-23
    • 2017-09-30
    • 2015-04-18
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    相关资源
    最近更新 更多