Bomber
Bomber

Reputation: 10967

Firebase messaging, Failed to determine project ID for Messaging

How can I initialise my app for messaging? I keep getting the error

"Error: Failed to determine project ID for Messaging. Initialize the SDK with service account credentials or set project ID as an app option. Alternatively set the GOOGLE_CLOUD_PROJECT environment variable."

when I try and invoke messaging

const functions = require("firebase-functions");
const admin = require("firebase-admin");

const config = {
    type: "service_account",
    project_id: "xxx",
    private_key_id: "xxx",
    private_key:
        "-----BEGIN PRIVATE KEY-----xxx-----END PRIVATE KEY-----\n",
    client_email:
        "firebase-adminsdk-8dd2o@pushmessage-bd1eb.iam.gserviceaccount.com",
    client_id: "107357357611247077666",
    auth_uri: "https://accounts.google.com/o/oauth2/auth",
    token_uri: "https://oauth2.googleapis.com/token",
    auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
    client_x509_cert_url:
        "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-8dd2o%40pushmessage-bd1eb.iam.gserviceaccount.com"
};

admin.initializeApp({
    config
});

const db = admin.firestore();
const messaging = admin.messaging();

const message = {
    data: { title: "Testing", body: "Test" },
    tokens: registrationTokens
};

messaging.sendMulticast(message).then(response => {
    console.log(response.successCount + " messages were sent successfully");
});

Upvotes: 4

Views: 9326

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317798

These lines don't make sense:

const db = admin.firestore(functions.config().firebase);
const messaging = admin.messaging(functions.config().firebase);

I'm not sure what you're intending to do by passing that parameter to admin.firestore() and admin.messaging().

If this is running in Cloud Functions, perhaps you meant to initialize the Admin SDK like this:

const admin = admin.initializeApp();

and use it like this:

const db = admin.firestore();
const messaging = admin.messaging();

Upvotes: 2

Related Questions