Android NotificationCompat.Builder()不接受通道Id作为参数
我知道这个问题以前被问过好几次。但没有一个解决方案对我有效。这就是为什么我想再次问这个问题。以下行仅接受NotificationCompat.Builder(上下文): 我实现了:Android NotificationCompat.Builder()不接受通道Id作为参数,android,android-notifications,android-8.1-oreo,notification-channel,Android,Android Notifications,Android 8.1 Oreo,Notification Channel,我知道这个问题以前被问过好几次。但没有一个解决方案对我有效。这就是为什么我想再次问这个问题。以下行仅接受NotificationCompat.Builder(上下文): 我实现了: 导入android.support.v4.app.NotificationCompat 我的支持库版本高于25 实现组:'com.android.support',名称:'appcompat-v7',版本:'27.1.1' 编译&目标sdk大于25 android{ 编译DK版本(27) buildToolsVer
- 导入
android.support.v4.app.NotificationCompat
- 我的支持库版本高于25
'实现组:'com.android.support',名称:'appcompat-v7',版本:'27.1.1
- 编译&目标sdk大于25
但仍然会出错。我应该怎么做才能修复此错误?请帮助android{ 编译DK版本(27) buildToolsVersion“27.0.3” “默认值” 数据绑定{ 启用=真 } 默认配置{ applicationId('something') 明斯克版本(16) targetSdkVersion(27) 版本代码(1) versionName('1.0.0') TestInstrumentRunner('android.support.test.runner.AndroidJUnitRunner') 多索引启用真 }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, ADMIN_CHANNEL_ID)
}
else NotificationCompat.Builder(context)
首先创建通知通道和通知传递通道ID
NotificationManager notificationManager = (NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);
buildNotificationChannel(manager,String <Channel_ID>,String description);
创建通知传递参数通道ID
Notification notification = new
NotificationCompat.Builder(context,<Channel_ID>)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Title goes here.")
.setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(false)
.setOngoing(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentText("Content Text").build();
notificationManager.notify(ID,notification);
通知=新建
NotificationCompat.Builder(上下文)
.setSmallIcon(R.drawable.app_图标)
.setContentTitle(“标题位于此处”)
.setPriority(通知。优先级高)
.setContentIntent(挂起内容)
.setAutoCancel(错误)
.正在进行(正确)
.setVisibility(通知.VISIBILITY\u公共)
.setContentText(“内容文本”).build();
notificationManager.notify(ID,通知);
解决方案:
在Android 8.0(Oreo)上,您必须拥有一个名为通知频道的功能,因此请尝试以下实现:
步骤1:创建通知频道
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
最后:然后您的通知:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(activity)
.setSmallIcon(R.drawable.ic_launcher_background) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(activity, RecorderActivity.class);
PendingIntent pi = PendingIntent.getActivity(activity,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
notificationManager.notify(1, mBuilder.build());
请将此与您的进行比较,看看是否有效
试试这个,希望有帮助。尝试更新你的gradle依赖项。
我也没有得到setChannelId方法或参数的值。
但正如它出现在支持库的更新版本中一样,您会找到它。
试用
implementation'com.android.support:support-v4:28.0.0'
发送错误日志。编译时出错。Builder(上下文)无法应用于Builder(上下文,java.lang.String)
您在哪个API级别上运行此操作?另外,为了匹配CompileSDK版本和buildToolsVersion,您的targetSDK版本不应该是27吗?首先,您也必须发送您的编译错误。其次,您有另一个生成程序出错。我尝试将targetSDK版本设置为27。不起作用。(NotificationCompat.Builder()
不接受第二个参数。
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(activity)
.setSmallIcon(R.drawable.ic_launcher_background) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(activity, RecorderActivity.class);
PendingIntent pi = PendingIntent.getActivity(activity,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
notificationManager.notify(1, mBuilder.build());