How to Send Push Notifications with Firebase Cloud Messaging
To send a push notification with
Firebase Cloud Messaging (FCM), use the send() method from the Firebase Admin SDK with a message object containing the target device token and notification details. This sends the notification to the specified device instantly.Syntax
The basic syntax to send a push notification using Firebase Admin SDK is:
admin.messaging().send(message): Sends the message object.message: An object containingtoken(device token),notification(title and body), and optionaldatafields.
javascript
const message = { token: '<DEVICE_TOKEN>', notification: { title: 'Hello', body: 'This is a push notification' }, data: { key1: 'value1', key2: 'value2' } }; admin.messaging().send(message) .then(response => { console.log('Successfully sent message:', response); }) .catch(error => { console.log('Error sending message:', error); });
Example
This example shows how to initialize Firebase Admin SDK and send a push notification to a device token with a title and body.
javascript
const admin = require('firebase-admin'); // Initialize Firebase Admin SDK with service account admin.initializeApp({ credential: admin.credential.cert(require('./serviceAccountKey.json')) }); const message = { token: 'YOUR_DEVICE_TOKEN', notification: { title: 'Greetings', body: 'You have a new message!' } }; admin.messaging().send(message) .then(response => { console.log('Successfully sent message:', response); }) .catch(error => { console.error('Error sending message:', error); });
Output
Successfully sent message: projects/your-project-id/messages/1234567890
Common Pitfalls
- Using an invalid or expired device
tokenwill cause the send to fail. - Not initializing Firebase Admin SDK properly with correct credentials causes authentication errors.
- Sending notifications without required
titleorbodyfields may result in silent failures. - Confusing client SDK and Admin SDK methods; only Admin SDK can send messages server-side.
javascript
/* Wrong: Missing initialization */ admin.messaging().send({ token: 'token' }); // Error: admin not initialized /* Right: Initialize first */ const serviceAccount = require('./serviceAccountKey.json'); admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }); admin.messaging().send({ token: 'token', notification: { title: 'Hi', body: 'Hello' } });
Quick Reference
Remember these key points when sending push notifications with Firebase:
- Use Firebase Admin SDK on your server.
- Provide a valid device
token. - Include
notificationwithtitleandbody. - Handle success and error responses to confirm delivery.
Key Takeaways
Use Firebase Admin SDK's send() method with a message object to send push notifications.
Always initialize Firebase Admin SDK with valid service account credentials before sending.
Include a valid device token and notification title and body in the message.
Handle errors to catch invalid tokens or authentication issues.
Client SDKs cannot send push notifications; use Admin SDK on the server.