When I first integrated push notifications into my project, I assumed Firebase Cloud Messaging (FCM) was only for Android. But after some research, I discovered that Firebase Admin SDK can send push notifications to both iOS and Android devices, saving me from managing two separate notification services.
In this article, I’ll share how I set up Firebase Admin SDK in Node.js to send push notifications to iOS and Android apps seamlessly.
Why Firebase Admin SDK?
Firebase Admin SDK is a powerful tool that simplifies sending notifications via Firebase Cloud Messaging (FCM). You don’t need to maintain a separate notification system for iOS and Android — just one codebase.
What You’ll Learn:
✅ How to send push notifications using Firebase Admin SDK.
✅ iOS and Android setup differences.
✅ Common pitfalls and how to avoid them.
Step 1: Setting Up Firebase Admin SDK in Node.js
Before we start, make sure your project is already set up with Firebase and that you’ve linked your Android and iOS apps in the Firebase Console.
Install Firebase Admin SDK:
npm install firebase-admin
Initialize Firebase Admin SDK:
import admin from 'firebase-admin';
// Initialize Firebase Admin
admin.initializeApp({
credential: admin.credential.cert({
type: 'service_account',
project_id: process.env.FIREBASE_PROJECT_ID,
private_key_id: process.env.FIREBASE_PRIVATE_KEY_ID,
private_key: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
client_email: process.env.FIREBASE_CLIENT_EMAIL,
client_id: process.env.FIREBASE_CLIENT_ID,
auth_uri: 'https://accounts.google.com/o/oauth2/auth',
token_uri: 'https://oauth2.googleapis.com/token',
auth_provider_x509_cert_url: 'https://www.googleapis.com/oauth2/v1/certs',
client_x509_cert_url: process.env.FIREBASE_CLIENT_X509_CERT_URL,
}),
});
Step 2: Configuring Your Mobile Apps
For Android:
- Add the Firebase SDK to your project.
- Register the device token using Firebase.
For iOS:
- Add Firebase SDK and enable Push Notifications in Xcode.
- Upload your APNs Authentication Key or Certificate in Firebase Console.
Step 3: Sending Notifications with Firebase Admin SDK
Here’s how to send a push notification from your Node.js server:
const sendNotification = async (fcmToken, title, body) => {
const message = {
notification: {
title: title,
body: body,
},
token: fcmToken, // Device FCM token
};
try {
const response = await admin.messaging().send(message);
console.log('Successfully sent message:', response);
} catch (error) {
console.error('Error sending message:', error);
}
};
// Example FCM Token (Replace with a real one)
const testToken = 'YOUR_DEVICE_FCM_TOKEN';
// Send a Test Notification
sendNotification(testToken, 'Hello World!', 'This is a test message.');
Best Practices to Ensure Delivery
- Check Your Firebase Setup:
Ensure APNs Authentication Key is uploaded in Firebase Console (for iOS). - Handle Device Token Registration:
Register tokens correctly in your mobile apps. - Ask for Notification Permission (iOS):
Ensure you ask for permission when the app is installed. - Use Production Keys for Production:
Use APNs Production Key for live deployments.
Common Pitfalls to Avoid
๐ฅ Wrong FCM Token: Ensure the mobile app properly registers the FCM token.
๐ฅ Misconfigured Firebase Project: Double-check that iOS and Android apps are linked in the Firebase Console.
๐ฅ Incorrect APNs Key Upload: If notifications don’t work on iOS, confirm your APNs key upload.
Final Thoughts
By using Firebase Admin SDK, I reduced the complexity of sending notifications to iOS and Android devices. Firebase Admin makes push notifications easy and reliable, even for cross-platform apps. If you’re building a project that requires push notifications, give Firebase Admin SDK a try — you won’t regret it.
Happy coding! ๐๐ฑ๐ฌ