我知道这个问题已经被问过几次了。但是,没有一种解决方案对我有用。这就是为什么我想再次问这个问题。以下行仅接受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
}
但是仍然出现错误。我应该怎么做才能解决这个问题?请帮忙。
答案 0 :(得分:1)
解决方案:
在Android 8.0(Oreo)上,您必须具有称为NotificationChannel
的名称,因此请尝试以下实现:
第一步:创建通知频道
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());
请与您的产品进行比较,看看是否有效
尝试一下,希望对您有所帮助。
答案 1 :(得分:0)
据我所知,您只能为大于或等于26的版本添加通知通道,低于android 8.0的版本不支持通知通道。我的建议是使用以下代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, ADMIN_CHANNEL_ID)
}
else NotificationCompat.Builder(context)
答案 2 :(得分:0)
首先创建通知通道和通知传递通道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);
答案 3 :(得分:0)
尝试更新gradle依赖项。 我也没有得到setChannelId方法或参数。 但是随着支持库的更新版本而来。你会找到它的。 尝试使用
implementation 'com.android.support:support-v4:28.0.0'