Android Notification Channel — Complete Guide
In this tutorial, you'll learn about Android Notification Channel. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Notifications don't show up on Android 8+ because you never created a notification channel, or they show without sound because you set the wrong importance.
Wrong Approach ❌
// No channel — notification won't show on API 26+
val builder = NotificationCompat.Builder(this, "general")
.setContentTitle("Hello")
.setContentText("World")
.setSmallIcon(R.drawable.ic_notification)
// Channel created but wrong importance
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
"alerts", "Alerts", NotificationManager.IMPORTANCE_MIN // No sound!
)
notificationManager.createNotificationChannel(channel)
}
Output: Notification doesn't appear (no channel) or appears without sound/title.
Right Approach ✅
class App : Application() {
override fun onCreate() {
super.onCreate()
createNotificationChannels()
}
private fun createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = getSystemService(NotificationManager::class.java)
// High importance — makes sound, shows heads-up
val urgentChannel = NotificationChannel(
URGENT_CHANNEL_ID,
"Urgent Alerts",
NotificationManager.IMPORTANCE_HIGH
).apply {
description = "Time-sensitive notifications"
enableVibration(true)
setLightColor(Color.RED)
}
// Default importance — shows in shade, no sound
val defaultChannel = NotificationChannel(
DEFAULT_CHANNEL_ID,
"Notifications",
NotificationManager.IMPORTANCE_DEFAULT
).apply {
description = "General notifications"
}
// Low importance — no sound, no heads-up
val silentChannel = NotificationChannel(
SILENT_CHANNEL_ID,
"Silent Updates",
NotificationManager.IMPORTANCE_LOW
).apply {
description = "Updates without interruption"
setSound(null, null)
}
// Create channel group
val group = NotificationChannelGroup("social", "Social")
notificationManager.createNotificationChannelGroup(group)
notificationManager.createNotificationChannels(
listOf(urgentChannel, defaultChannel, silentChannel)
)
}
}
}
Output: Notifications show with correct sound, vibration, and appearance.
Prevention
- Create all channels in
Application.onCreate()before posting any notification. - Use
IMPORTANCE_HIGHfor urgent,IMPORTANCE_DEFAULTfor normal,IMPORTANCE_LOWfor silent. - Use channel groups for categorizing related channels.
- Channel importance cannot be changed programmatically — only by the user.
Common Mistakes with notification channel
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro