Skip to content

Firebase FCM Notification Display — Complete Guide

DodaTech Updated 2026-06-24 2 min read

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() and setColorized(true) for branded notifications.
  • Use setGroup() for notification grouping per conversation.

Common Mistakes with Firebase fcm notification

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. 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

### Why does my notification icon appear as a white square?

You need to use an adaptive icon (vector drawable with transparent background) for setSmallIcon(). White icons with transparent backgrounds render correctly on all versions.

### How do I group notifications from the same sender?

Use setGroup(groupKey) on all notifications and create one summary notification with setGroupSummary(true). The group key should be the conversation or sender ID.

### What is setColorized?

It's an API 26+ feature that fills the notification's background with the provided color. This creates a more branded appearance for collapsed notifications.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro