fcm notification node js code example
Example 1: node js send fcm
var FCM = require('fcm-node')
var serverKey = require('path/to/privatekey.json') //put the generated private key path here
var fcm = new FCM(serverKey)
var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
to: 'registration_token',
collapse_key: 'your_collapse_key',
notification: {
title: 'Title of your push notification',
body: 'Body of your push notification'
},
data: { //you can send only notification or only data(or include both)
my_key: 'my value',
my_another_key: 'my another value'
}
}
fcm.send(message, function(err, response){
if (err) {
console.log("Something has gone wrong!")
} else {
console.log("Successfully sent with response: ", response)
}
})
Example 2: import firebase cloud messaging web
import ReactDOM from "react-dom";import App from "./App";if ("serviceWorker" in navigator) { navigator.serviceWorker .register("./firebase-messaging-sw.js") .then(function(registration) { console.log("Registration successful, scope is:", registration.scope); }) .catch(function(err) { console.log("Service worker registration failed, error:", err); });}ReactDOM.render(, document.getElementById("root"));