直接上代码
//设置通知通道
private fun notifyChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = ConstantStr.NOTIFICATION_CHANNEL_ID
val channelName = ConstantStr.NOTIFICATION_CHANNEL_NAME
val importance = NotificationManager.IMPORTANCE_HIGH
createNotificationChannel(channelId, channelName, importance)
}
}
@TargetApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(channelId:String, channelName:String, importance:Int) {
val notificationManager = getSystemService(
NOTIFICATION_SERVICE) as NotificationManager
var channel = notificationManager.getNotificationChannel(ConstantStr.NOTIFICATION_CHANNEL_ID)
if(channel == null){
channel = NotificationChannel(channelId, channelName, importance)
channel.setShowBadge(true)//显示红点提示
channel.description = ConstantStr.NOTIFICATION_CHANNEL_DESCRIPTION
channel.enableLights(true)
channel.enableVibration(true)
channel.setShowBadge(true)
channel.lightColor = Color.GREEN
notificationManager.createNotificationChannel(channel)
}
}
在主Activity调用即可
参考文章