Junior — Middle
Have you had experience working with the NotificationChannel mechanism for sending notifications?
sobes.tech AI
Answer from AI
NotificationChannel is a mechanism introduced in Android 8.0 (API 26) for managing notifications. It allows grouping notifications into channels with different importance levels, sounds, and visual effects. When sending a notification, you need to specify the channel; otherwise, the notification will not be shown.
Example of creating a channel:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
"channel_id",
"Main Channel",
NotificationManager.IMPORTANCE_DEFAULT
).apply {
description = "Channel description"
}
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
After that, when creating a notification, specify this channel:
val notification = NotificationCompat.Builder(context, "channel_id")
.setContentTitle("Title")
.setContentText("Notification text")
.setSmallIcon(R.drawable.ic_notification)
.build()
notificationManager.notify(notificationId, notification)