Im trying to learn how to read the official firebase docs. but I cant seem to get it right
I want to send notifications from a cloud function to a phone, but I was only able to do it using some functions not found on the docs ( see code below )
I know the docs say to use getMessaging().send(message), but I cant get it to work {see image attached}
code where I am able to send notification
const functions = require("firebase-functions");
const admin = require("firebase-admin");
// eslint-disable-next-line max-len
const tokens = ["REDACTED_TOKEN"];
admin.initializeApp();
exports.onCreate = functions.firestore
.document("chat/{docId}")
.onCreate((snapshot, context) => {
console.log(snapshot.data());
console.log("fake data");
});
exports.onUpdate = functions.firestore
.document("chat messages/{docId}")
.onCreate( (snapshot, context) => {
const payload = {
// eslint-disable-next-line max-len
notification: {title: "Push Title", body: "Push Body", sound: "default"},
// eslint-disable-next-line max-len
data: {click_action: "FLUTTER_NOTIFICATION_CLICK", message: "Sample Push Message"},
};
try {
admin.messaging().sendToDevice(tokens, payload);
console.log("NOTIFICATION SEND SUCCESFULLY");
} catch (e) {
console.log("ERROR SENDING NOTIFICATION");
console.log(e);
}
});
[2] https://firebase.google.com/docs/cloud-messaging/send-message