Firebase CloudFunctions:查询节点
Firebase CloudFunctions: Querying Nodes
我正在使用 FCM 探索 Cloud Functions,以便向我的 iOS 应用设备发送推送通知。我有以下结构,我正在听注册活动的人。我想提取 eventHost,这样我就可以去我的用户节点找到 userUID 并最终找到他的 deviceID,并向他发送推送通知。
{
"events" : {
"XXX" : {
"eventHost" : "YYY", //<-- How do I get this node?
"eventID" : "XXX",
"registered" : { //<-- Listening for this node
"ASKDJHAIUCHA" : {
"name" : "Emma Watson",
"userUID" : "ASKDJHAIUCHA"
}
},
},
},
"users" : {
"YYY" : {
"deviceID" : "1234456",
"name" : "Andrew Garfield",
"userUID" : "YYY"
},
}
}
到目前为止,我的 Cloud Functions 代码:
exports.sendNotification = functions.database.ref('/events/{eventId}/registered')
.onWrite(event => {
const register = event.data.val();
const eventHost = functions.database.ref('/events/' + event.params.eventId + '/eventHost')
console.log('sendNotifications', eventHost);
const payload = {
notification: {
title: "Event Registration",
body: "Someone registered to your event!"
}
};
const options = {
priority: "high"
};
return admin.messaging().sendToDevice("theDeviceID", payload, options)
});
我的 Swift 将值添加到数据库时的部分是这样的:
@IBAction func registerButtonDidTap(_ sender: Any) {
let personDict = [FIRConstants.UserInfo.Name: user.userName,
FIRConstants.UserInfo.UserUID: user.userUID]
let registerPerson = [user.userUID!: personDict as AnyObject] as [String: AnyObject]
let values = ["registered": registerPerson]
FIRHelperClient.sharedInstance.checkIfEventHasRegistrants(ref, event.eventID!) { (has, error) in
if let error = error {
print(error.localizedDescription)
} else {
if let has = has {
if has {
self.ref.child("events").child(self.event.eventID!).child("registered").updateChildValues(registerPerson)
} else {
self.ref.child("events").child(self.event.eventID!).updateChildValues(values)
}
}
}
}
}
我的云功能绝对不完整,因为目前我正在对 DeviceID 进行硬编码。由于我对 Node.js 非常缺乏经验,并且正在尝试在 Swift 中编写 iOS 代码和服务器端代码,所以如果这个问题很初级,请原谅我。这里的一些建议会有所帮助,谢谢。
您需要读取主机,而您的代码目前不执行此操作。
exports.sendNotification = functions.database.ref('/events/{eventId}/registered')
.onWrite(event => {
const register = event.data.val();
const eventHostRef = functions.database.ref('/events/' + event.params.eventId + '/eventHost')
return eventHostRef.once('value', (eventHostSnapshot) => {
const eventHost = eventHostSnapshot.val();
console.log('sendNotifications', eventHost);
const payload = {
notification: {
title: "Event Registration",
body: "Someone registered to your event!"
}
};
const options = {
priority: "high"
};
return admin.messaging().sendToDevice("theDeviceID", payload, options)
});
});
我强烈建议您花一些时间学习如何通过 JavaScript 与 Firebase 数据库交互,然后再继续。这不必通过 Cloud Functions。你也可以 use the Firebase Database Admin SDK from Node.js on your client or take the Firebase codelab for web。无论您选择哪一个,都将确保您为通过 Cloud Functions 与数据库交互做好更充分的准备。
作为最后的警告:您在单个列表下嵌套了多种数据类型。不推荐这样做,因为它会导致各种问题。相反,我会将注册用户拉到他们自己的顶级节点中,这样你会得到:
{
"events" : {
"XXX" : {
"eventHost" : "YYY",
"eventID" : "XXX"
}
},
"registered" : {
"XXX" : {
"ASKDJHAIUCHA" : {
"name" : "Emma Watson",
"userUID" : "ASKDJHAIUCHA"
}
}
},
"users" : {
"YYY" : {
"deviceID" : "1234456",
"name" : "Andrew Garfield",
"userUID" : "YYY"
}
}
}
我正在使用 FCM 探索 Cloud Functions,以便向我的 iOS 应用设备发送推送通知。我有以下结构,我正在听注册活动的人。我想提取 eventHost,这样我就可以去我的用户节点找到 userUID 并最终找到他的 deviceID,并向他发送推送通知。
{
"events" : {
"XXX" : {
"eventHost" : "YYY", //<-- How do I get this node?
"eventID" : "XXX",
"registered" : { //<-- Listening for this node
"ASKDJHAIUCHA" : {
"name" : "Emma Watson",
"userUID" : "ASKDJHAIUCHA"
}
},
},
},
"users" : {
"YYY" : {
"deviceID" : "1234456",
"name" : "Andrew Garfield",
"userUID" : "YYY"
},
}
}
到目前为止,我的 Cloud Functions 代码:
exports.sendNotification = functions.database.ref('/events/{eventId}/registered')
.onWrite(event => {
const register = event.data.val();
const eventHost = functions.database.ref('/events/' + event.params.eventId + '/eventHost')
console.log('sendNotifications', eventHost);
const payload = {
notification: {
title: "Event Registration",
body: "Someone registered to your event!"
}
};
const options = {
priority: "high"
};
return admin.messaging().sendToDevice("theDeviceID", payload, options)
});
我的 Swift 将值添加到数据库时的部分是这样的:
@IBAction func registerButtonDidTap(_ sender: Any) {
let personDict = [FIRConstants.UserInfo.Name: user.userName,
FIRConstants.UserInfo.UserUID: user.userUID]
let registerPerson = [user.userUID!: personDict as AnyObject] as [String: AnyObject]
let values = ["registered": registerPerson]
FIRHelperClient.sharedInstance.checkIfEventHasRegistrants(ref, event.eventID!) { (has, error) in
if let error = error {
print(error.localizedDescription)
} else {
if let has = has {
if has {
self.ref.child("events").child(self.event.eventID!).child("registered").updateChildValues(registerPerson)
} else {
self.ref.child("events").child(self.event.eventID!).updateChildValues(values)
}
}
}
}
}
我的云功能绝对不完整,因为目前我正在对 DeviceID 进行硬编码。由于我对 Node.js 非常缺乏经验,并且正在尝试在 Swift 中编写 iOS 代码和服务器端代码,所以如果这个问题很初级,请原谅我。这里的一些建议会有所帮助,谢谢。
您需要读取主机,而您的代码目前不执行此操作。
exports.sendNotification = functions.database.ref('/events/{eventId}/registered')
.onWrite(event => {
const register = event.data.val();
const eventHostRef = functions.database.ref('/events/' + event.params.eventId + '/eventHost')
return eventHostRef.once('value', (eventHostSnapshot) => {
const eventHost = eventHostSnapshot.val();
console.log('sendNotifications', eventHost);
const payload = {
notification: {
title: "Event Registration",
body: "Someone registered to your event!"
}
};
const options = {
priority: "high"
};
return admin.messaging().sendToDevice("theDeviceID", payload, options)
});
});
我强烈建议您花一些时间学习如何通过 JavaScript 与 Firebase 数据库交互,然后再继续。这不必通过 Cloud Functions。你也可以 use the Firebase Database Admin SDK from Node.js on your client or take the Firebase codelab for web。无论您选择哪一个,都将确保您为通过 Cloud Functions 与数据库交互做好更充分的准备。
作为最后的警告:您在单个列表下嵌套了多种数据类型。不推荐这样做,因为它会导致各种问题。相反,我会将注册用户拉到他们自己的顶级节点中,这样你会得到:
{
"events" : {
"XXX" : {
"eventHost" : "YYY",
"eventID" : "XXX"
}
},
"registered" : {
"XXX" : {
"ASKDJHAIUCHA" : {
"name" : "Emma Watson",
"userUID" : "ASKDJHAIUCHA"
}
}
},
"users" : {
"YYY" : {
"deviceID" : "1234456",
"name" : "Andrew Garfield",
"userUID" : "YYY"
}
}
}