android - 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());
}
}
解决方案
您正在使用NotificationCompat
which 需要(来自支持库)的支持版本。RemoteInput
您当前使用的是不支持的版本RemoteInput
,因此出现错误。
只需import android.app.RemoteInput
从类的顶部删除。当系统提示您选择要使用的导入时,请选择支持版本,它应该可以工作。或者您可以手动将其替换为import android.support.v4.app.RemoteInput
.
推荐阅读
- java - 如何将 HttpClient 与任何 ssl 证书一起使用,无论它有多“糟糕”
- git - Git通过清除reflog撤消所有更改?
- mainframe - 使用 SORT 从输入文件中提取数据到不提供预期数据的不同输出文件
- android - 配置变更时如何观察 WorkManager?
- python-3.x - 我如何区分 log2(2^5910-1) 和 log2(2^5910)?两者都给 5909.9999999999
- node.js - CastError:转换为嵌入的值失败
- bash - 用于打印包含用户添加的单词的行的 Shell 脚本
- typo3 - TYPO3 9.5 带有损坏链接的简单 TMENU
- angular - Angular - 未捕获的错误导致重定向回基本 URL
- javascript - 通过按钮将 UTM 参数传递到另一个页面