If you’re building a web or mobile app and need real-time, scalable push notifications, Firebase Cloud Messaging (FCM) is a powerful and free solution provided by Google. This step-by-step guide walks you through setting up Firebase to deliver notifications reliably to your users — whether on Android, iOS, or web.
✅ Why Choose Firebase for Notifications?
- Free and scalable (supports millions of devices)
- Cross-platform support (iOS, Android, Web)
- Built-in message targeting
- Integration with Firebase Analytics & Cloud Functions
🔧 Step 1: Set Up Firebase Project
- Go to Firebase Console
- Click “Add Project”, enter a name and follow the setup wizard
- Once created, go to Project Settings > Cloud Messaging
📲 Step 2: Integrate FCM in Your App
For Android:
- Add google-services.json to your /app folder
- Update build.gradle files to include Firebase dependencies:
implementation 'com.google.firebase:firebase-messaging'
- Initialize Firebase in your app’s Application class or MainActivity
For iOS:
- Add GoogleService-Info.plist to your project
- Enable push notifications and background modes in Xcode
- Use CocoaPods:
pod 'Firebase/Messaging'
- Request APNs token and register for remote notifications
For Web:
- Include Firebase SDK:
<script src="https://www.gstatic.com/firebasejs/10.0.0/firebase-app.js"></script> <script src="https://www.gstatic.com/firebasejs/10.0.0/firebase-messaging.js"></script>
- Initialize Firebase with your config and call:
const messaging = firebase.messaging();
🛠 Step 3: Request Permission & Get Device Token
messaging.requestPermission().then(() => { return messaging.getToken(); }).then(token => { // Send token to your backend });
This token is essential to send targeted notifications to that specific device.
⚙️ Step 4: Send Notifications
Option 1: Use Firebase Console
- Go to Firebase Console > Cloud Messaging > New Notification
- Target users by token, topic, or user segment
Option 2: Use Admin SDK (Backend)
const admin = require('firebase-admin'); admin.messaging().send({ token: userToken, notification: { title: 'Hello!', body: 'You’ve got a new message.' } });
⚡️ Step 5: Handle Notifications in the App
Foreground:
messaging.onMessage((payload) => { console.log('Message received:', payload); });
Background/Closed:
Notifications will be handled automatically unless you customize them using a service worker (for web) or native methods (Android/iOS).
🔁 Optional: Subscribe to Topics
Allow users to subscribe to groups:
admin.messaging().subscribeToTopic([token], 'news-updates');
Then send to topic:
admin.messaging().send({ topic: 'news-updates', notification: { title: 'Breaking News!', body: 'Check out the latest update.' } });
🔒 Best Practices
- Store tokens securely and refresh regularly
- Handle token changes using onTokenRefresh
- Use topics for broadcasting to many users
- Monitor delivery success via Firebase Analytics
Firebase makes real-time, cross-platform notifications simple, powerful, and scalable. Whether you’re notifying a single user or millions, it just works.
👉 Need help integrating FCM or building full-stack notification systems?
Connect with Atisolve — your expert partner in scalable software development.