【问题标题】:Firebase messaging, message not recievedFirebase 消息,未收到消息
【发布时间】:2019-04-11 10:52:50
【问题描述】:

我的云功能显示已成功发送消息undefined messages were sent successfully,但我没有收到:

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

var serviceAccount = require("./config.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://pushmessage-bd1eb.firebaseio.com"
});

const messaging = admin.messaging();
const message = {
            data: { title: "Testing", body: "Test" },
            token:
                "fm8hZocb9X0:APA91bGANY8U1k7iXSAofh8PEtyA3SfkAvyvicjHbSzDC7s1DwzhCxBBhj5oeAhiZpNLFC1wUHOPX_C0vlGtUMv882EXxBjsM4qeBpFndka8kzir9kgmJnuPTRImx2cxUT53oXzJuAzB"
        };

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

如果我在 firebase 仪表板中使用相同的令牌发送消息,则消息发送成功。

如何让我的云功能发送消息?

config.json:

{
  "type": "service_account",
  "project_id": "pushmessage-bd1eb",
  "private_key_id": "xxx",
  "private_key": "-----BEGIN PRIVATE KEY-----\nxxx-----END PRIVATE KEY-----\n",
  "client_email": "firebase-adminsdk-8dd2o@pushmessage-bd1eb.iam.gserviceaccount.com",
  "client_id": "xxx",
  "senderID": "388436954224",
  "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"
}

【问题讨论】:

    标签: firebase firebase-cloud-messaging


    【解决方案1】:

    你在使用 admin.messaging().send() 吗?

    见:

    请尝试以下代码。

    data 更改为notification

    而 admin.messaging().send() 返回字符串。

          const message = {
            notification: { title: "Testing", body: "Test" },
            token:
                "The registration token here"
          };
    
          admin.messaging().send(message).then(response => {
            console.log(
                response + " messages were sent successfully"
            );
          });
    

    或者你可以使用 admin.messaging().sendToDevice()。

    见:

    请尝试以下代码。

          const token= "The registration token here";
          const payload = {
            notification: { title: "Testing", body: "Test" }
          };
    
          admin.messaging().sendToDevice(token, payload).then(response => {
            console.log(
                response.successCount + " messages were sent successfully"
            );
          });
    

    【讨论】:

    • 是的,我正在使用 admin.messaging,我收到错误,UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:1):错误:未找到请求的实体。
    猜你喜欢
    • 2018-05-15
    • 2020-01-21
    • 2018-01-03
    • 2018-04-24
    • 2017-12-24
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多