尝试向 FCM 服务器进行身份验证时出错

UA *_*DEV 2 push-notification firebase firebase-cloud-messaging

尝试向 FCM 服务器进行身份验证时出错。确保用于验证此 SDK 的凭据具有适当的权限。有关设置说明, 请参阅https://firebase.google.com/docs/admin/setup在此处输入图片说明

Pes*_*san 91

为了解决这个问题,我采取了以下步骤:

  1. 打开 Google Cloud Platform 仪表板
  2. 转到 API 和服务
  3. 启用 API 和服务
  4. 搜索云消息传递
  5. 打开云消息传递和 Firebase 云消息传递 API。

  • 这解决了我的问题。“Firebase 云消息传递” API 已启用。我必须启用“云消息”API。谢谢@Peshraw Hasan (17认同)
  • 非常感谢卡马苏保罗。我很惊讶文档中没有说明这一点。即使使用云消息传递的过滤器也不会显示此选项。您必须在顶部搜索云消息传递,然后启用它。之后一切都按预期进行。 (2认同)

Abh*_*ire 17

默认情况下,Firebase 使用 Firebase Cloud Messaging API (V1),因此您需要使用此 api 而不是旧版 API

对于 Firebase 云消息 API (V1):

const data = {
    message: {
      token: registrationToken,
      notification: {
        title: "Notification Title",
        body: "Notification Body ",
      },
      data: {
        Nick: "Mario",
        Room: "PortugalVSDenmark",
      },
    },
  };

  admin.messaging().send(data.message);
Run Code Online (Sandbox Code Playgroud)

对于 Cloud Messaging API(旧版) 1.首先转到 Google Cloud Platform Dashboard 并启用 Cloud Messaging Service 2.然后您可以像这样使用:

 var payload = {
    notification: {
      title: "This is a Notification",
      body: "This is the body of the notification message.",
    },
  };

  var options = {
    priority: "high",
  };

  var registrationToken ="your device token";
   admin
     .messaging()
     .sendToDevice(registrationToken, payload, options)
    .then(function (response) {
       console.log("Successfully sent message:", response);
     })
     .catch(function (error) {
      console.log("Error sending message:", error);
     });
Run Code Online (Sandbox Code Playgroud)

建议使用 Firebase Cloud Messaging API (V1),而不是 Cloud Messaging API(旧版)


UA *_*DEV 12

下载新的私钥并删除旧的!

在此输入图像描述


小智 9

就我而言,按照Abhimanyu的解决方案生成新密钥仍然无法解决问题。

经过 5 个小时的挖掘,我找到了 Google 没有为我的项目激活此 API 的根本原因!!!!

在此输入图像描述

激活云消息权限后就可以了。


Gui*_*sch 6

我刚刚遇到这个问题,对我来说,这是因为我使用的是模拟器。模拟器生成的 FCM 令牌对 firebase 云消息传递无效,并且它抛出了这个错误。

我把我的admin.messaging().sendToDevice()变成了一个try catch,它工作正常。

我不知道它是否仅向有效令牌发送通知,或者它是否完全忽略了所有令牌