1

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."

  1. I tried solving it by providing absolute/relative paths, never worked.

  2. Did deleted the npm_modules/lock file and still no solution as per this.

  3. 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.

  4. Cannot deploy Firebase Functions using `initializeapp`

Ali Wajdan
  • 13
  • 2

1 Answers1

0

You have two issues:

Misconfiguration in your code

Remove this line:

const serviceAccount = require("/service.json");

You do not need to specify any credentials for Cloud Functions. They run in the context of their runtime service account by default and no authentication is required.

Then change this line:

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://...-default-rtdb.asia-southeast1.firebasedatabase.app",
});

To this:

admin.initializeApp();

Because again, this context is picked up automatically within the Functions Framework and you do not have to specify it.


Missing API configuration

  1. Open Google Cloud Platform Dashboard
  2. Go to API and Services
  3. Enable API and Services
  4. Search for Cloud Messaging
  5. Turn on the Cloud Messaging and Firebase Cloud Messaging API

If the problem persists then ensure that the runtime service account for your Cloud Function has been granted the cloudmessaging.messages.create IAM permission.

anothermh
  • 9,815
  • 3
  • 33
  • 52