private fun createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channelName = getString(R.string.app_name)
val descriptionText = getString(R.string.channel_description)
val channel = NotificationChannel(
NotificationID.CHANNEL_ID,
channelName,
NotificationManager.IMPORTANCE_MIN
).apply {
description = descriptionText
}
channel.enableLights(false)
channel.enableVibration(false)
channel.setSound(null, null)
channel.setShowBadge(false)
notificationManager.createNotificationChannel(channel)
}
}
fun createNotification() {
val notificationCustomView = notificationCustomView(R.layout.notification_show)
val notification = NotificationCompat.Builder(this, NotificationID.CHANNEL_ID)
.setSmallIcon(R.mipmap.icon)
.setLargeIcon( BitmapFactory.decodeResource(
resources,
R.mipmap.icon
))
.setCustomBigContentView(notificationCustomView)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setGroupSummary(false)
.setGroup( getString(R.string.app_name))
.setContent(notificationCustomView)
.setAutoCancel(false)
.setSilent(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setOngoing(true) //通知栏常驻
.build()
with(NotificationManagerCompat.from(this)) {
notify(NotificationID.NOTIFICATION_ID, notification)
}
}
网友评论