尝试认证到FCM服务器时发生错误。

31
6个回答

83
为了解决这个问题,我采取了以下步骤:
  1. 打开Google Cloud Platform仪表板
  2. 进入API和服务
  3. 启用API和服务
  4. 搜索云消息传递
  5. 启用云消息传递和Firebase云消息传递API。

14
这解决了我的问题。Firebase云消息传送(Firebase cloud messaging)API已经启用,我只需要启用云消息传送(cloud messaging)API即可。谢谢@Peshraw Hasan。 - kamasuPaul
1
对我也起作用了。我不知道除了 Firebase 云消息传递之外,还必须启用常规的 Google 云消息传递。 - Eric
2
非常感谢您,kamasuPaul。我很惊讶文档中没有提到这一点。即使使用云消息过滤器也不会显示此选项。您必须在顶部搜索云消息,然后启用它。之后一切都按预期工作。 - function1983
1
Firebase应该在文档中提到这一点,感谢Peshraw!! - Zubeir
为什么谷歌只激活了 Firebase 消息传递,而不是同时启用两者? - Clode Morales Pampanga III
显示剩余2条评论

17

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

Firebase Cloud Messaging API(V1)的用法:

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

  admin.messaging().send(data.message);

使用 Cloud Messaging API (Legacy): 1.首先前往 Google Cloud 平台仪表盘并启用 Cloud Messaging 服务 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);
     });

推荐使用Firebase Cloud Messaging API (V1)而不是Cloud Messaging API (Legacy)


这对我也起作用了。如果您使用的是较新的API,则我认为界面已更改。 - d512
非常感谢。到2023年,API默认为FCM v1。因此,按照@Abhishek指出的相关API是正确的方法。 - Stéphane de Luca

14

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

我将我的admin.messaging().sendToDevice()放入了一个try catch中,现在它能够正常工作了。

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


12

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

输入图像描述


1
我一直在想如何进入图像中的屏幕。最终,我按照链接中的文档弄清楚了。 - Dipen
你如何在代码中使用这个键? - Zorayr

8

在我的情况下,按照Abhimanyu的解决方案生成新密钥仍然无法解决问题。

经过5个小时的挖掘,我发现问题的根本原因是Google没有为我的项目激活此API!

enter image description here

在我激活云消息权限之后,它就能正常工作了。


这个有效。 - Rajesh
但这是遗留版本,Firebase建议不要使用。 - Junius L

2

以下是针对 @Abhishek Ghimire 提出的 FCM v1 使用方式的澄清。

下面是创建消息的方法:

  const message = {
      token: registrationToken,
      notification: {
        title: "Notification Title",
        body: "Notification Body ",
      },  
  };

  admin.messaging().send(message);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接