NotificationCompat与API 26

我没有看到有关如何使用Android O的Notification Channels NotificationCOmpat的任何信息

我看到一个新的构造函数,它需要一个channelId但是如何获取Compat通知并在NotificationChannel中使用它,因为createNotificationChannel需要一个NotificationChannel对象

Solutions Collecting From Web of "NotificationCompat与API 26"

仅当API> = 26时才创buildNotificationChannel

 public void initChannels(Context context) { if (Build.VERSION.SDK_INT < 26) { return; } NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationChannel channel = new NotificationChannel("default", "Channel name", NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("Channel description"); notificationManager.createNotificationChannel(channel); } 

然后只使用:

 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default"); 

所以你的通知是API 26(带通道)和低于(不带)。

声明通知pipe理器:

  final NotificationManager mNotific= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); CharSequence name="Ragav"; String desc="this is notific"; int imp=NotificationManager.IMPORTANCE_HIGH; final String ChannelID="my_channel_01"; 

通知频道

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel mChannel = new NotificationChannel(ChannelID, name, imp); mChannel.setDescription(desc); mChannel.setLightColor(Color.CYAN); mChannel.canShowBadge(); mChannel.setShowBadge(true); mNotific.createNotificationChannel(mChannel); } final int ncode=101; String Body="This is testing notific"; 

通知生成器

  Notification n= new Notification.Builder(this,ChannelID) .setContentTitle(getPackageName()) .setContentText(Body) .setBadgeIconType(R.mipmap.ic_launcher) .setNumber(5) .setSmallIcon(R.mipmap.ic_launcher_round) .setAutoCancel(true) .build(); 

NotificationManager通知用户:

  mNotific.notify(ncode, n);