首页 > 解决方案 > Android 通知生成器问题将远程输入添加到操作

问题描述

我正在尝试向我的应用程序中的通知添加直接回复功能。按照此处的文档(https://developer.android.com/training/notify-user/build-notification#add-reply-action

我收到以下错误: addRemoteInput(android.support.v4.app.RemoteInput) in Builder cannot be applied to (android.app.RemoteInput)

我似乎无法找到问题的解释。我想知道文档是否有问题?

在此处输入图像描述

public void android8ChatMethod(PendingIntent pendingIntent, String channelID, String contentTitle, String contentText) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //Create notification builder
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.stat_notify_chat)
                .setContentTitle("Inline Reply Notification");

        int randomRequestCode = new Random().nextInt(54325);

        //PendingIntent that restarts the current activity instance.
        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //Set a unique request code for this pending intent
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, randomRequestCode, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        String replyLabel = "Enter your reply here";

        //Initialise RemoteInput
        RemoteInput remoteInput = new RemoteInput.Builder(KEY_REPLY)
                .setLabel(replyLabel)
                .build();

        //Notification Action with RemoteInput instance added.
        NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
                android.R.drawable.sym_action_chat, "REPLY", resultPendingIntent)
                .addRemoteInput(remoteInput)
                .setAllowGeneratedReplies(true)
                .build();

        //Notification.Action instance added to Notification Builder.
        builder.addAction(replyAction);

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("notificationId", 1234);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent dismissIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.addAction(android.R.drawable.ic_menu_close_clear_cancel, "DISMISS", dismissIntent);

        //Create Notification.
        NotificationManager notificationManager =
                (NotificationManager)
                        getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(1234,
                builder.build());
    }
}

标签: androidpush-notification

解决方案


您正在使用NotificationCompatwhich 需要(来自支持库)的支持版本。RemoteInput您当前使用的是不支持的版本RemoteInput,因此出现错误。

只需import android.app.RemoteInput从类的顶部删除。当系统提示您选择要使用的导入时,请选择支持版本,它应该可以工作。或者您可以手动将其替换为import android.support.v4.app.RemoteInput.


推荐阅读