Firebase Cloud Messaging Intro

Firebase Cloud Messaging (FCM) sends push notifications to users' devices — even when they are not actively using your app. A message can appear as a notification banner on a phone or a notification popup in a browser. FCM delivers these messages reliably across Android, iOS, and web browsers.

How Push Notifications Work

Your server (or Cloud Function)
       |
       | Sends message with FCM token
       v
Firebase Cloud Messaging servers
       |
       | Routes to the right platform
       +---> Apple Push Notification Service (iOS)
       +---> Google Firebase (Android)
       +---> Web Push Protocol (browsers)
       |
       v
User's device displays the notification

FCM Tokens

An FCM token is a unique identifier for a specific app installation on a specific device. To send a notification to a user, you need their device's current FCM token. Tokens change when the user reinstalls the app, clears app data, or resets the device, so always store the latest token.

FCM Token example:
eWLf2bX1234ABC...xyz789 (long random string)

This token identifies:
- Your specific app
- On this specific device
- For this specific user installation

Types of FCM Messages

Notification Messages

Firebase automatically displays notification messages as system notifications on the device. When the app is in the background, the system handles display. When the app is in the foreground, you handle display in your code.

Notification message payload:
{
  notification: {
    title: "New message",
    body: "Alice sent you a message"
  },
  token: "user-device-token"
}

Data Messages

Data messages deliver a custom payload to your app silently, without displaying a system notification. Your app's code receives the payload and decides what to do with it — display a custom notification, update the UI, or trigger a background sync.

Data message payload:
{
  data: {
    action: "new_order",
    order_id: "ord_12345",
    amount: "999"
  },
  token: "user-device-token"
}

Setting Up FCM in the Firebase Console

FCM is available in your Firebase project by default — no separate activation is required. The main setup tasks happen in your app code and when sending messages. For web push notifications, you need one additional step: generating a VAPID key.

Go to Project Settings > Cloud Messaging. Scroll to Web configuration and click Generate key pair under Web Push certificates. Firebase generates a VAPID key — a public key used by browsers to verify that push messages come from your project. Copy this key for use in your web app setup.

Sending a Test Notification

Send a test notification from the Firebase Console without writing any server code:

  1. Go to Cloud Messaging in the console
  2. Click Send your first message
  3. Enter a notification title and body
  4. Click Send test message
  5. Paste an FCM token (retrieved from your app) and click Test

The notification appears on the device registered with that token.

FCM Delivery Guarantees

FCM delivers messages with these characteristics:

  • At-least-once delivery — messages are retried if delivery fails initially
  • Order not guaranteed — messages may arrive slightly out of order
  • TTL (Time to Live) — you set how long FCM tries to deliver (default 28 days)
  • Collapse key — send only the latest message if multiple queue up while the device is offline

FCM and User Privacy

Browsers and mobile operating systems require users to grant notification permission before receiving push notifications. Apps must request permission explicitly, and users can revoke it at any time from their device settings. Always request notification permission at a moment that makes sense in the user's journey — not immediately on first launch.

Key Takeaway

Firebase Cloud Messaging delivers push notifications to Android, iOS, and web browsers. Each device has a unique FCM token that identifies where to send messages. Notification messages display automatically as system notifications; data messages are delivered silently to your app code. Generate a VAPID key in the console for web push notifications. Always request notification permission at the right moment in the user journey, and store updated FCM tokens in Firestore when they change.

Leave a Comment

Your email address will not be published. Required fields are marked *