当然,这是官方声明的创建方法,我们还有其他的创建方法,下面的是我在百度上看到的方法
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //创建通知渠道实例 //并为它设置属性 //通知渠道的ID,随便写 String id = "channel_01"; CharSequence name = getString(R.string.app_name); //用户可看到的通知描述 String description = getString(R.string.app_name); //构建NotificationChannel实例 NotificationChannel notificationChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT); //配置通知渠道的属性 notificationChannel.setDescription(description); //设置通知出现时的闪光灯 notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); //设置通知出现时的震动 notificationChannel.enableVibration(true); notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 100}); //在notificationManager中创建通知渠道 manager.createNotificationChannel(notificationChannel); Notification notification = new NotificationCompat.Builder(MainActivity.this, id) .setContentTitle("标题") .setContentText("内容666666666666666666666666666") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .build(); manager.notify(1, notification);