0
votes

I want to my Node.js server to be able to automatically send firebase cloud messages to devices with specific registration tokens. The docs discuss several ways I can authorize my Node.js server, including relying on the default project authorization if I am deploying with Google Cloud (which I am). However, while I'm testing my Node.js server on my local machine, it seems like I need to set an environment variable with the path of a downloaded configuration .json.

I did this with:

export GOOGLE_APPLICATION_CREDENTIALS="/Users/johnsorensen/Documents/Ops Docs/notificationsmrp-firebase-adminsdk-h86ml-cd2d8d8f7a.json"

I can verify that this indeed the correct file path, as I had Mac's finder return it to me.

When I run my app, however, I get this error:

errorInfo: {
    code: 'app/invalid-credential',
    message: 'Failed to determine project ID: Error while making request: getaddrinfo 
ENOTFOUND metadata.google.internal. Error code: ENOTFOUND'
  },
  codePrefix: 'app'

Node.js code:

const admin = require("firebase-admin")

admin.initializeApp({ credential: admin.credential.applicationDefault(),})



const registrationToken = 'ePdAwFHd_EBJufZC6XQU8l:APA91bEYD5pFKIMuPwO7oAlKWqbENUXXG_TsLf67IS1Lyf-ge9szJ5Eqo0W8GTqgyheXSyNrNY6_QbU5V8nq39hj42cgphRG9PClgKOY8Ugc6aOFMTTY9t5pWrV_suzvzEXH2cprm3Nv';

const message = {
    data: {
        score: '850',
        time: '2:45'
    },
    token: registrationToken
};


admin.messaging().send(message)
    .then((response) => {
        console.log("Success")
        console.log(response)
    })
    .catch((error) => {
        console.log("Error Sending Message", error)
    })
I see a comma ',' after credential: admin.credential.applicationDefault() in admin.initializeApp. Was this intentional? If you are not passing any parameters like projectId,databaseURL keep it only admin.initializeApp({ credential: admin.credential.applicationDefault()}) Or if you want to specify projectId and other parameters, follow this documentation - Priyashree Bhadra