匿名用户
像下面这样创建频道,Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
String characterSequence = "Message Alert";
NotificationChannel channel = new NotificationChannel(channelId,
characterSequence,NotificationManager.IMPORTANCE_DEFAULT);
channel.setSound(soundUri,audioAttributes );
NotificationManager manager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
}
您需要像下面这样在这里传递频道id,NotificationCompat.Builder builder= new NotificationCompat.Builder(APITest2.this,channelId)
另请参阅https://developer.android.com/training/notify-user/channels
如果您需要显示heads up通知,那么您需要将通知通道中的重要性级别设置为importance_high。 并且需要在notification builder中设置优先级,如下所示。setPriority(NotificationCompat.PRIORITY_HIGH);