代码之家  ›  专栏  ›  技术社区  ›  noob-Sci-Bot

NotificationCompat.Builder()不接受通道Id作为参数

  •  0
  • noob-Sci-Bot  · 技术社区  · 7 年前

    我知道这个问题以前被问过好几次。但没有一个办法对我有效。所以我想再问一次这个问题。下一行只接受 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 '

    • 编译(&A);目标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 }

    3 回复  |  直到 7 年前
        1
  •  2
  •   Ümañg ßürmån    7 年前

    在android8.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());
    

    试试这个,希望有帮助。

        2
  •  0
  •   Sifat Ullah Chowdhury    7 年前

    据我所知,您只能为大于或等于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)
    
        3
  •  0
  •   swati kapoor    6 年前

    尝试更新gradle依赖项。 但是当它出现在支持库的更新版本中时。你会找到的。 尝试使用

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

        4
  •  -1
  •   Ramesh Yankati    7 年前

      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);
    
    推荐文章