I'm a bit noob to Flutter and working my way through it. I'm working on an Application in Flutter that sends a Push Notification when a user wants to send it to a user.
The function is written in Node JS (I actually don't know much of it so might be a problem with my code). Here's the code.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
// const creds = "/Users/raeesali/Downloads/service.json";
const serviceAccount = require("/service.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://udhar-app-cb137-default-rtdb.asia-southeast1.firebasedatabase.app",
});
exports.sendNotification = functions.https.onCall((data, context) => {
try {
const token = data.token;
const payload = {
notification: {
title: data.title,
body: data.body,
clickAction: "FLUTTER_ACTION_CLICK",
},
};
return admin.messaging().sendToDevice(token, payload).then((response) => {
return {
success: true,
response: `Successful ${response}`,
};
}).catch((error) => {
return {
success: false,
response: `Error ${error}`,
};
});
} catch (error) {
console.log(`Some Error Occurred ${error}`);
return {
success: false,
response: `Error ${error}`,
};
}
});
However, when I try to deploy this function I get the error saying:
"Error: Failed to load function definition from source: Failed to generate manifest from function source: Error: Cannot find module '/service.json'"
File does exists, I've tried even providing it with absolute path still not works. However, as per the several tutorials if I only use the admin.initializeApp() line, then the function is deployed successfully and when the function is triggered I get this response as an exception.
"Error: An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions."
I tried solving it by providing absolute/relative paths, never worked.
Did deleted the npm_modules/lock file and still no solution as per this.
Checked my gpc according to this one and everything was enabled, except that I never exactly found Cloud Messaging to enable/disable rather Firebase Cloud Messaging.