NotificationCompat.Builder() 不接受频道 ID 作为参数
NotificationCompat.Builder() not accepting Channel Id as argument
我知道这个问题已经被问过好几次了。但是 none 的解决方案对我有用。这就是为什么我想再次提出这个问题。以下行只接受 NotificationCompat.Builder(context)
:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, ADMIN_CHANNEL_ID) // Getting error
我已经完成:
- 导入
android.support.v4.app.NotificationCompat
我的支持库版本高于 25
implementation group: 'com.android.support', name: 'appcompat-v7', version: '27.1.1
'
编译 & 目标 sdk 高于 25
android {
compileSdkVersion(27)
buildToolsVersion '27.0.3'
flavorDimensions 'default'
dataBinding {
enabled = true
}
defaultConfig {
applicationId('something')
minSdkVersion(16)
targetSdkVersion(27)
versionCode(1)
versionName('1.0.0')
testInstrumentationRunner('android.support.test.runner.AndroidJUnitRunner')
multiDexEnabled true
}
但仍然收到错误。我应该怎么做才能解决这个问题?请帮忙
据我所知只能为大于等于26的版本添加通知通道,android8.0以下的版本不支持通知通道。我的建议是使用以下代码:
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);
创建通知渠道。
public void buildNotificationChannel(NotificationManager manager, String channelID,
String description) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (manager.getNotificationChannel(channelID) == null) {
NotificationChannel channel = new NotificationChannel(channelID,
description,NotificationManager.IMPORTANCE_LOW);
channel.setDescription(description);
manager.createNotificationChannel(channel);
}
}
}
创建通知传递参数通道 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);
解法:
在 Android 8.0 (Oreo) 上你必须有一个叫做 NotificationChannel
的东西所以试试下面的实现:
步骤 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'
我知道这个问题已经被问过好几次了。但是 none 的解决方案对我有用。这就是为什么我想再次提出这个问题。以下行只接受 NotificationCompat.Builder(context)
:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, ADMIN_CHANNEL_ID) // Getting error
我已经完成:
- 导入
android.support.v4.app.NotificationCompat
我的支持库版本高于 25
implementation group: 'com.android.support', name: 'appcompat-v7', version: '27.1.1
'编译 & 目标 sdk 高于 25
android { compileSdkVersion(27) buildToolsVersion '27.0.3' flavorDimensions 'default' dataBinding { enabled = true } defaultConfig { applicationId('something') minSdkVersion(16) targetSdkVersion(27) versionCode(1) versionName('1.0.0') testInstrumentationRunner('android.support.test.runner.AndroidJUnitRunner') multiDexEnabled true }
但仍然收到错误。我应该怎么做才能解决这个问题?请帮忙
据我所知只能为大于等于26的版本添加通知通道,android8.0以下的版本不支持通知通道。我的建议是使用以下代码:
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);
创建通知渠道。
public void buildNotificationChannel(NotificationManager manager, String channelID,
String description) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (manager.getNotificationChannel(channelID) == null) {
NotificationChannel channel = new NotificationChannel(channelID,
description,NotificationManager.IMPORTANCE_LOW);
channel.setDescription(description);
manager.createNotificationChannel(channel);
}
}
}
创建通知传递参数通道 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);
解法:
在 Android 8.0 (Oreo) 上你必须有一个叫做 NotificationChannel
的东西所以试试下面的实现:
步骤 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'