I have a cloud function that takes an fcmToken and sends a push notification to the right device.
The .then is triggered and the console log inside as well, but the Success html never shows. Instead, it always goes to the catch and shows the Failure html with an empty error.
Where am I going wrong?
Function code:
const {onRequest} = require("firebase-functions/v2/https");
const admin = require("firebase-admin");
const serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
exports.sendNotification = onRequest((request, response) => {
const token = request.params[0].replace("/", "");
const registrationToken = token;
console.log("Token: " + token);
const message = {
notification: {
title: "Hello",
body: "body text...",
},
token: registrationToken,
};
admin.messaging().send(payload)
.then((response) => {
console.log("Successfully sent message:", JSON.stringify(response));
response.send(`<h1>Success</h1>`);
})
.catch((error) => {
const errorString = JSON.stringify(error);
console.log("Error sending message:", errorString);
response.send(`<h1>Failure: ${errorString}</h1>`);
});
});