3

我正在使用 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);
}
4

1 回答 1

0

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

于 2022-02-18T20:06:19.853 回答