尝试向 FCM 服务器进行身份验证时出现错误。确保用于对此 SDK 进行身份验证的凭据具有适当的权限。请参阅 https://firebase.google.com/docs/admin/setup 获取设置说明。
输入图像描述
默认情况下,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)
我刚遇到了这个问题,对于我来说是因为我正在使用模拟器。模拟器生成的FCM令牌对于Firebase云消息传递来说无效,并且会抛出此错误。
我将我的admin.messaging().sendToDevice()
放入了一个try catch
中,现在它能够正常工作了。
我不知道它是否只向有效的令牌发送通知,或者完全忽略所有令牌。
以下是针对 @Abhishek Ghimire 提出的 FCM v1 使用方式的澄清。
下面是创建消息的方法:
const message = {
token: registrationToken,
notification: {
title: "Notification Title",
body: "Notification Body ",
},
};
admin.messaging().send(message);
Firebase云消息传送(Firebase cloud messaging)
API已经启用,我只需要启用云消息传送(cloud messaging)
API即可。谢谢@Peshraw Hasan。 - kamasuPaul