2

我知道这个问题已经被问过好几次了。但是没有一个解决方案对我有用。这就是为什么我想再次问这个问题。以下行仅接受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 } 但仍然得到错误。我应该怎么做才能解决这个问题?请帮忙。

4

4 回答 4

2

解决方案:

在 Android 8.0 (Oreo) 上,你必须有一个叫做 a 的东西NotificationChannel所以试试下面的实现:

Step1:创建通知通道

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());

请将此与您的进行比较,看看它是否有效

试试这个,希望它有帮助。

于 2018-11-04T08:29:15.507 回答
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)
于 2018-11-04T08:03:14.257 回答
0

尝试更新您的 gradle 依赖项。我也没有得到 setChannelId 方法或参数。但是因为它出现在支持库的更新版本中。你会找到它的。尝试使用

implementation 'com.android.support:support-v4:28.0.0'

于 2019-09-25T07:53:08.823 回答
-1

首先创建通知通道和通知传递通道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);
于 2018-11-04T08:08:19.733 回答