问题
I know this question has been asked several times before. But none of the solutions worked for me. That's why I would like to ask the question again. The following line only accepts NotificationCompat.Builder(context)
:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, ADMIN_CHANNEL_ID) // Getting error
I have fulfilled:
- importing
android.support.v4.app.NotificationCompat
My support libraries version is above 25
implementation group: 'com.android.support', name: 'appcompat-v7', version: '27.1.1
'Compile & Target sdk is above 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 }
But still getting the error. What should I do to fix this? Please help.
回答1:
Solution:
On Android 8.0 (Oreo) you must have something called as a NotificationChannel
So try the below implementation:
Step1: Create Notification Channel
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);
}
}
Finally: Then your Notification:
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());
Please compare this with Yours and see if it works
Try this, Hope it helps.
回答2:
As far as I know you can add notification channel only for versions greater than or equal 26, the versions below android 8.0 doesn't support notification channel. My suggestion would be using following codes:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, ADMIN_CHANNEL_ID)
}
else NotificationCompat.Builder(context)
回答3:
First create notification channel and notification passing channel ID.
NotificationManager notificationManager = (NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);
buildNotificationChannel(manager,String <Channel_ID>,String description);
Create Notification channel.
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);
}
}
}
Create Notification passing argument 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);
回答4:
Try updating your gradle dependencies. I was also not getting the the setChannelId method or the arguments. But as it came in the updated version of the support library. You will find it. Try using
implementation 'com.android.support:support-v4:28.0.0'
来源:https://stackoverflow.com/questions/53138762/notificationcompat-builder-not-accepting-channel-id-as-argument