我正在尝试发送注册通知到我的应用程序后,注册和用户已保存在数据库中。为此,我使用了firebase云函数。
我已经从firebaseinstanceidservice获取了设备令牌,并将其保存在用户的数据库中,路径为users/userid/deviceToken
,并在如下代码所示的函数中引用了此路径:
exports.sendNotification = functions.database.ref("users/{userId}/{instanceId_token}")
我试着记录设备令牌,只是为了确保安全,但在云控制台中,我不断获得该日志的其他属性,如:sendNotification deviceToken name
,sendNotification deviceToken userId
,而不是保存在数据库中的字母数字值。这条路是不是错了?
下面是我的完整代码:
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客户端,我该如何解决这个问题?
下面是我的db结构:
发布于 2018-06-02 20:27:50
您应该在上面的节点上触发函数,如下所示。并通过changes.after.val()
访问instanceId_token
。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification =
functions.database.ref("users/{userId}")
.onWrite((changes, context) => {
const userId = context.params.userId;
console.log("user-id", userId);
const notificationToken = changes.after.val().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)
.catch(function (error) {
console.log("Error sending message: ", error);
})
});
如果您只在创建用户之后添加instanceId_token
,那么您应该使用onUpdate()
触发(它“在数据更新时触发”,而onWrite()
“在创建、更新或删除数据时触发”)。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification =
functions.database.ref("users/{userId}")
.onUpdate((changes, context) => {
const userId = context.params.userId;
console.log("user-id", userId);
const notificationToken = changes.after.val().instanceId_token;
console.log("deviceToken", notificationToken);
if (notificationToken === undefined) {
console.log("notificationToken === undefined");
return false;
} else {
var payload = {
data: {
title: "Welcome to My Group",
message: "You may have new messages"
}
};
return admin.messaging().sendToDevice(notificationToken, payload)
.catch(function (error) {
console.log("Error sending message: ", error);
})
}
});
还要注意,您不应该这样做
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);
});
因为在这种情况下,您不会返回“在函数中完成所有异步工作时解析的承诺”。(参见此question的答案)
所以只需返回sendToDevice返回的promise (参见doc),如下所示:
return admin.messaging().sendToDevice(notificationToken, payload)
.catch(function (error) {
console.log("Error sending message: ", error); // here no return
});
https://stackoverflow.com/questions/50655844
复制相似问题