How to send message from Node server, not by FCM console?
Sending_Firebase_Cloud_Messages_from_a_Node.js_Server
Prepare necessary data
- Get your Firebase DB URL
- Open Firebase project
- Create new Realtime Database
- Get Firebase Admin private key
- Get a device fcmToken
Debug react-native client app to get fcmToken
Create new node project
- Configure node project
npm init yarn add firebase-admin
- Create index.js file
var admin = require("firebase-admin"); var serviceAccount = require(${`your_firebase_admin_private_key_file_path`}); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: ${`your_firebase_db_url`}, }); var registrationToken =${`your_device_fcm_token`}; var options = { priority: "high", timeToLive: 60 * 60 * 24, }; var payload = { notification: { title: "Testing Notification", body: "This is my first notification", }, data: { account: "Savings", balance: "$3020.25", }, }; admin .messaging() .sendToDevice(registrationToken, payload, options) .then(function (response) { console.log("Successfully sent message:", response); }) .catch(function (error) { console.log("Error sending message:", error); });
Leave a Reply