Firebase消息传递,无法确定消息传递的项目ID

时间:2019-04-05 15:20:59

标签: firebase firebase-cloud-messaging

如何初始化我的应用以进行消息传递?我不断收到错误

“错误:无法确定用于消息传递的项目ID。使用服务帐户凭据初始化SDK或将项目ID设置为应用选项。或者设置GOOGLE_CLOUD_PROJECT环境变量。”

当我尝试调用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");
});

1 个答案:

答案 0 :(得分:0)

这些行没有意义:

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

通过将参数传递到admin.firestore()admin.messaging(),我不确定您打算做什么。

如果此命令正在Cloud Functions中运行,则可能是要像这样初始化Admin SDK:

const admin = admin.initializeApp();

并像这样使用它:

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