【问题标题】:Unable to send FCM notification to device using NodeJS Admin SDK无法使用 NodeJS Admin SDK 向设备发送 FCM 通知
【发布时间】:2021-11-09 17:33:40
【问题描述】:

我正在按照docs 中提到的代码通过 Admin SDK 发送通知。

exports.sendNotification = functions.https.onRequest((req, res) => {
  const message = {
    data: {
      type: "warning",
      content: "A new weather warning has been created!",
    },
    topic: "weather",
  };
  
  admin
    .messaging()
    .send(message)
    .then((response) => {
      console.log("Successfully sent message:", response);
    })
    .catch((error) => {
      console.log("Error sending message:", error);
    });
    res.end();
}); 

使用 Firebase 模拟器运行上述代码后,控制台打印

i  functions: Beginning execution of "us-central1-sendNotification"
i  functions: Finished "us-central1-sendNotification" in ~1s
>  Successfully sent message: projects/foo/messages/2216986345254434321

但是,我在设备上看不到任何通知。

注意:如果我通过 Firebase Notification composer 或 Postman 将通知发送到同一主题 weather,则设备会显示通知。不知道上面的代码有什么问题。

【问题讨论】:

    标签: android node.js flutter firebase-cloud-messaging google-cloud-messaging


    【解决方案1】:

    您正在发送data message,这需要您自己处理显示。

    结帐Message Types 了解不同类型的 FCM 消息。

    要查看您的通知,您可以执行以下任一操作:

    • 发送notification message 而不是数据消息,这意味着您将在message 有效负载对象中使用notification 键代替data 键,如下所示:
    const message = {
        notification: {
          title: "warning",
          body: "A new weather warning has been created!",
        },
        topic: "weather",
    };
    

    【讨论】:

    • 我不知道 HTTP 协议和管理 SDK 以类似的方式工作。谢谢它的工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多