我有一个非常简单的火力函数:
exports.sendFollowNotification = functions.database.ref('PendingRequest/{receiver_id}/{sender_id}').onWrite(requestEvent => {
const requestSnapShot = requestEvent.data;
const senderId = requestEvent.params.sender_id;
const targetId = requestEvent.params.receiver_id;
const target_token = requestSnapShot.child('sender').val();
const sender_token = requestSnapShot.child('receiver').val();
console.log('sender_id :'+senderId);
console.log('target_id :'+targetId);
console.log('target_token: '+ target_token);
console.log('sender_token: '+sender_token);
const pendingRequestPayload = {
data: {
token_sender : sender_token,
token_target : target_token,
request_sender : senderId,
request_receiver : targetId,
my_message_id: '0'
}
};
if(target_token != null){
// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToDevice(target_token, pendingRequestPayload)
.then(function (response) {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log("Successfully sent message:", response);
})
.catch(function (error) {
console.log("Error sending message:", error);
});
}
每当这个函数触发时,有两个值被交换:senderId 获取 targetId 值,反之亦然。这两个值都是使用 params 属性检索的,而我从 requestSnapShot.child('value_name'(.val(( 获得的值没有任何奇怪的变化;
愚蠢的解决方案只是在需要时交换这两个值,但是,这是一个非常愚蠢的解决方案。我在这里错过了什么?
如果"目标"是"接收器",则交换这些:
const target_token = requestSnapShot.child('sender').val();
const sender_token = requestSnapShot.child('receiver').val();
您是否有意这样做以解决问题?
更新:
很难猜测为什么这对你不起作用。我复制了您的代码,消除了您的解决方法,并缩短了它以进行测试:
exports.sendFollowNotification = functions.database.ref('PendingRequest/{receiver_id}/{sender_id}')
.onWrite(requestEvent => {
const requestSnapShot = requestEvent.data;
const senderId = requestEvent.params.sender_id;
const targetId = requestEvent.params.receiver_id;
const target_token = requestSnapShot.child('receiver').val();
const sender_token = requestSnapShot.child('sender').val();
console.log('sender_id :'+senderId);
console.log('target_id :'+targetId);
console.log('target_token: '+ target_token);
console.log('sender_token: '+sender_token);
});
使用此数据运行:
{
"PendingRequest" : {
"R1" : {
"S1" : {
"receiver" : "R-token",
"sender" : "S-token"
}
}
}
}
并得到了这个日志输出:
sender_token: S-token
target_token: R-token
target_id :R1
sender_id :S1