Firebase FCM Notification Display — Complete Guide
In this tutorial, you'll learn about Firebase FCM Notification Display. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your FCM notification shows with the default icon or no icon, or multiple notifications from the same sender don't group.
Wrong Approach ❌
// Relying entirely on server-side notification payload
// Server sends: { "notification": { "title": "...", "body": "..." } }
// Client does nothing — system shows it with default icon
Output: Notification shows with default white icon or no icon at all.
Right Approach ✅
class MyFcmService : FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage) {
val data = message.data
// Use data payload for full control
val notificationId = data["notification_id"]?.toIntOrNull() ?: System.currentTimeMillis().toInt()
val groupKey = data["group_key"] ?: "default"
// Create group summary for multiple notifications
if (data["is_group_summary"] == "true") {
val summary = NotificationCompat.Builder(this, FCM_CHANNEL_ID)
.setContentTitle("${data["title"]} (${data["count"]})")
.setContentText(data["body"])
.setSmallIcon(R.drawable.ic_notification)
.setGroup(groupKey)
.setGroupSummary(true)
.setAutoCancel(true)
.build()
notify(notificationId, summary)
return
}
// Individual notification
val notification = NotificationCompat.Builder(this, FCM_CHANNEL_ID)
.setContentTitle(data["title"] ?: "Notification")
.setContentText(data["body"] ?: "")
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(loadBitmapFromUrl(data["image_url"]))
.setGroup(groupKey)
.setGroupAlertBehavior(GroupAlertBehavior.GROUP_ALERT_ALL)
.setAutoCancel(true)
.setContentIntent(createPendingIntent(data))
.build()
notify(notificationId, notification)
}
// Set notification icon colors
private fun styledNotification() {
// You can also use NotificationCompat.Builder styling:
.setColor(ContextCompat.getColor(this, R.color.notification_accent))
.setColorized(true) // API 26+ — uses color for icon background
}
}
Output: Properly styled notifications with correct icon and grouping.
Prevention
- Always handle FCM messages on the client for full control over notification appearance.
- Use
setSmallIcon()with a proper adaptive icon. - Use
setColor()andsetColorized(true)for branded notifications. - Use
setGroup()for notification grouping per conversation.
Common Mistakes with Firebase fcm notification
- Mixing let bindings with <- bindings in do notation, producing type errors
- 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
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