How to Implement FCMP — Step-by-Step Tutorial

How to Implement FCMP — Step-by-Step Tutorial

Note: I’ll assume FCMP refers to Firebase Cloud Messaging (FCM) with the commonly used typo “FCMP.” If you meant a different FCMP, tell me and I’ll adapt the guide.

Overview

Firebase Cloud Messaging (FCM) lets you send push notifications to Android, iOS, and web clients. This tutorial shows a minimal, practical implementation: project setup, server key, client setup (Android and Web), sending a test message from a Node.js server, and verifying delivery.

Prerequisites

  • Firebase project (console access)
  • Node.js installed (for sending messages)
  • Android Studio (for Android client) or a web development environment
  • Basic familiarity with JSON and REST

1. Create a Firebase project and enable FCM

  1. Open the Firebase Console and create a new project.
  2. Add an Android app (package name) and/or a Web app.
  3. Download the configuration file:
    • Android: google-services.json — place in app/ of your Android project.
    • Web: copy the firebaseConfig object from the SDK setup snippet.

2. Obtain server credentials

  1. In Firebase Console, go to Project Settings > Cloud Messaging.
  2. Copy the Server key (for legacy API) or create a Service Account:
    • For production, create a Service Account key (JSON) in Google Cloud IAM & Admin > Service Accounts, grant role “Firebase Admin” or “Cloud Messaging Admin”, and download the JSON. Use this with Firebase Admin SDK (preferred).

3. Android client setup (minimal)

  1. In your Android app module build.gradle:
    • Apply plugin: com.google.gms.google-services
    • Add dependencies:
    implementation ‘com.google.firebase:firebase-messaging:23.1.1’
  2. Place google-services.json in app/.
  3. Create a FirebaseMessagingService subclass:
    public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { // Handle message and show notification } @Override public void onNewToken(String token) { // Send token to your server }}
  4. Register the service in AndroidManifest or via automatic manifest merging.
  5. Request runtime notification permissions on Android 13+ if targeting SDK 33.

4. Web client setup (minimal)

  1. Include Firebase scripts and initialize with firebaseConfig:
    import { initializeApp } from “https://www.gstatic.com/firebasejs/9.22.1/firebase-app.js”;import { getMessaging, getToken, onMessage } from “https://www.gstatic.com/firebasejs/9.22.1/firebase-messaging.js”; const app = initializeApp(firebaseConfig);const messaging = getMessaging(app);
  2. Obtain permission and token:
    const vapidKey = “YOUR_WEB_PUSH_CERTIFICATE_KEY_PAIR_VAPID_KEY”;const token = await getToken(messaging, { vapidKey });// Send token to your server
  3. Add a service worker (firebase-messaging-sw.js) to handle background messages:

5. Server: sending messages with Firebase Admin SDK (Node.js)

  1. Install:
    npm install firebase-admin
  2. Initialize with service account JSON:
    const admin = require(‘firebase-admin’);const serviceAccount = require(‘./serviceAccountKey.json’); admin.initializeApp({ credential: admin.credential.cert(serviceAccount)});
  3. Send a message to a device token:
    const message = { token: registrationToken, notification: { title: ‘Hello’, body: ‘This is a test message’ }, data: { key1: ‘value1’ }}; admin.messaging().send(message) .then(response => console.log(‘Success:’, response)) .catch(error => console.error(‘Error:’, error));

6. Server: sending messages via HTTP v1 (alternative)

  1. Use OAuth2 access token generated from service account and POST to: https://fcm.googleapis.com/v1/projects/PROJECT_ID/messages:send
  2. Request body example:
    { “message”: { “token”: “”, “notification”: { “title”: “Hi”, “body”: “Hi body” } }}

7. Best practices and tips

  • Use topics for broadcasting: admin.messaging().sendToTopic(‘news’, payload).
  • Store and rotate device tokens; handle token refresh events.
  • Use data messages for silent/background handling and notification messages for visible alerts.
  • Limit payload size and batch messages where possible.

Comments

Leave a Reply

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