Notifications
Why notifications need channels
Since Android 8.0, every notification must belong to a channel — a category like “Messages” or “Promotions”. Users can then mute or customise each channel individually. You create channels once, usually at app start.
Create a channel
val channel = NotificationChannel(
"messages", "Messages", NotificationManager.IMPORTANCE_HIGH
).apply { description = "Chat messages" }
val manager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(channel)
Build and show a notification
val notification = NotificationCompat.Builder(this, "messages")
.setSmallIcon(R.drawable.ic_message)
.setContentTitle("New message")
.setContentText("Anand sent you a message")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true) // dismiss when tapped
.build()
NotificationManagerCompat.from(this).notify(1, notification)
Opening a screen when tapped
val intent = Intent(this, ChatActivity::class.java)
val pending = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_IMMUTABLE
)
// .setContentIntent(pending) on the builder
The Android 13+ permission
On Android 13 and newer you must also request the POST_NOTIFICATIONS runtime permission before notifications appear.
Common mistakes
- Forgetting to create the channel (nothing shows on Android 8+).
- Not requesting
POST_NOTIFICATIONSon Android 13+. - Spamming notifications — users will turn them off.
Summary: Create a channel, build withNotificationCompat, attach aPendingIntentto open a screen, and request the notifications permission on Android 13+.