I'm looking for guidance on how to send a Firebase Cloud Message through my Firebase Functions backend. I seem to be running into trouble with the payload and need help resolving it. Do I need an interface for the payload? Thank you in advance!
Error
Error sending message: { Error: Invalid JSON payload received. Unknown name "message" at 'message': Cannot find field.
at FirebaseMessagingError.FirebaseError [as constructor] (/home/ubuntu/environment/****/functions/node_modules/firebase-admin/lib/utils/error.js:42:28)
Notification Function (Recently Updated Code)
async function notification(
notificationType: string,
registrationToken: string,
objectText: string
) {
const matchesRef = db.collection("notifications");
const notificationObject = await matchesRef.doc(notificationType).get();
if (notificationObject.exists) {
const tokenMessage: admin.messaging.Message = {
token: registrationToken,
notification: {
title: notificationObject.data()!.title,
body: notificationObject.data()!.body
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
title: notificationObject.data()!.title,
body: notificationObject.data()!.body
},
android: {
priority: "high"
},
apns: {
headers: {
"apns-priority": "5"
}
}
};
admin
.messaging()
.send(tokenMessage)
.then((response: string) => {
// Response is a message ID string.
logMe(`Successfully sent message: ${response}`);
return response;
})
.catch((error: string) => {
console.log("Error sending message:", error);
});
}
return false;
}