Firebase云函数CanonicalRegistrationTokenCount出错
在db中保存注册和用户后,我正在尝试向我的应用发送注册通知。为此,我正在使用firebase云函数 我已从FirebaseInstancedService获取设备令牌,并将其保存在用户数据库中,路径为Firebase云函数CanonicalRegistrationTokenCount出错,firebase,google-cloud-functions,Firebase,Google Cloud Functions,在db中保存注册和用户后,我正在尝试向我的应用发送注册通知。为此,我正在使用firebase云函数 我已从FirebaseInstancedService获取设备令牌,并将其保存在用户数据库中,路径为users/userid/deviceToken,并在函数中引用此路径,如以下代码所示: exports.sendNotification = functions.database.ref("users/{userId}/{instanceId_token}") 我尝试记录devicetoken只
users/userid/deviceToken
,并在函数中引用此路径,如以下代码所示:
exports.sendNotification = functions.database.ref("users/{userId}/{instanceId_token}")
我尝试记录devicetoken只是为了确定,但在云控制台中,我不断获取该日志的其他属性,如:sendNotification
deviceToken名称
,发送通知
deviceToken用户ID
,而不是保存在db中的字母数字值。这条路错了吗
以下是我的完整代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification =
functions.database.ref("users/{userId}/{instanceId_token}")
.onWrite((changes, context) => {
const userId = context.params.userId;
console.log("user-id", userId);
const notificationToken = context.params.instanceId_token;
console.log("deviceToken", notificationToken);
var payload = {
data: {
title: "Welcome to My Group",
message: "You may have new messages"
}
};
return admin.messaging().sendToDevice(notificationToken, payload)
.then(function (response) {
return console.log("Successfully sent message: ", response);
})
.catch(function (error) {
return console.log("Error sending message: ", error);
})
});
此外,函数在admin.messaging
回调之后显示这条稍微积极的消息:
Successfully sent message: { results: [ { error: [Object] } ],
canonicalRegistrationTokenCount: 0,
failureCount: 1,
successCount: 0,
multicastId: 8880906162831350000 }
我使用的是android客户端,如何解决这个问题
以下是我的数据库结构:
您应该在上面的节点上触发您的函数,如下所示。并通过
changes.after.val()
访问instanceId\u令牌
如果您仅在创建用户后才添加instanceId\u令牌
,则应使用onUpdate()
(它“在数据更新时触发”,而onWrite()
“在数据创建、更新或删除时触发”)
还要注意的是,你不应该这样做
return admin.messaging().sendToDevice(notificationToken, payload)
.then(function (response) {
return console.log("Successfully sent message: ", response);
})
.catch(function (error) {
return console.log("Error sending message: ", error);
});
因为在这种情况下,您不会返回“在函数中完成所有异步工作时解决”的承诺。(见本节的答案)
因此,只需返回sendToDevice返回的承诺(请参阅),如下所示:
return admin.messaging().sendToDevice(notificationToken, payload)
.catch(function (error) {
console.log("Error sending message: ", error); // here no return
});
你能在你的问题中添加数据库的导出吗?我已经编辑了这个问题。现在看看。谢谢。它不工作,兄弟,第一个抛出错误,第二个没有显示通知。两种情况下都不会记录设备id。mmhh。。。我可以确认以下两个console.log
正确工作(console.log(“用户id”,userId);
和console.log(“deviceToken”,notificationToken);
)为onWrite打印正确的值,我已经测试过了。如果出现错误,则可能是sendToDevice()出错。您的代码对于这部分看起来正常,所以可能是由于错误的令牌造成的?对于第二种情况,当我将其包含在我的答案中并复制/粘贴了您自己的代码时,实际上我很匆忙(因此出现了错误)!我已经更新了答案。@Andromeda你能再试一次吗,我已经“微调”了我的答案。@Andromeda我知道你已经接受了答案,谢谢。如果你认为我的答案是恰当的,你介意点击帖子左侧的大向上箭头来将其向上投票吗?看见谢谢
return admin.messaging().sendToDevice(notificationToken, payload)
.then(function (response) {
return console.log("Successfully sent message: ", response);
})
.catch(function (error) {
return console.log("Error sending message: ", error);
});
return admin.messaging().sendToDevice(notificationToken, payload)
.catch(function (error) {
console.log("Error sending message: ", error); // here no return
});