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
- Open the Firebase Console and create a new project.
- Add an Android app (package name) and/or a Web app.
- 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
- In Firebase Console, go to Project Settings > Cloud Messaging.
- 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)
- 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’ - Place google-services.json in app/.
- 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 }} - Register the service in AndroidManifest or via automatic manifest merging.
- Request runtime notification permissions on Android 13+ if targeting SDK 33.
4. Web client setup (minimal)
- 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); - 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 - Add a service worker (firebase-messaging-sw.js) to handle background messages:
importScripts(’https://www.gstatic.com/firebasejs/9.22.1/firebase-app-compat.js’);importScripts(‘https://www.gstatic.com/firebasejs/9.22.1/firebase-messaging-compat.js’);firebase.initializeApp(firebaseConfig);const messaging = firebase.messaging();messaging.onBackgroundMessage(payload => { // Show notification});
5. Server: sending messages with Firebase Admin SDK (Node.js)
- Install:
npm install firebase-admin - Initialize with service account JSON:
const admin = require(‘firebase-admin’);const serviceAccount = require(‘./serviceAccountKey.json’); admin.initializeApp({ credential: admin.credential.cert(serviceAccount)}); - 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)
- Use OAuth2 access token generated from service account and POST to: https://fcm.googleapis.com/v1/projects/PROJECT_ID/messages:send
- 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.
Leave a Reply