Firebase CloudFunctions:查询节点

时间:2017-03-18 06:39:39

标签: node.js swift firebase firebase-cloud-messaging google-cloud-functions

我正在使用FCM探索云功能,以便向我的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"
    },
  }
}

到目前为止我的云功能代码:

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)

                    }
                }
            }
        }
    }

我的云功能肯定不完整,因为目前我正在硬编码设备ID。由于我对Node.js缺乏经验,并且我试图在Swift和服务器端代码中编写iOS代码,所以请原谅我,如果这个问题是基本的。这里的一些建议会有所帮助,谢谢。

1 个答案:

答案 0 :(得分:2)

您需要阅读主机,而您的代码目前还没有这样做。

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数据库进行交互。这不一定要通过云功能。您也可以从客户端的Node.js use the Firebase Database Admin SDK获取或the Firebase codelab for web。无论您采取哪种方式,它都将确保您通过云功能更好地与数据库进行交互。

作为最后警告:您在单个列表下嵌套多种数据类型。不建议这样做,因为它会导致各种问题。相反,我会将注册用户拉入他们自己的顶级节点,以便获得:

{
  "events" : {
    "XXX" : {
      "eventHost" : "YYY",
      "eventID" : "XXX"
    }
  },
  "registered" : {
    "XXX" : {
      "ASKDJHAIUCHA" : {
        "name" : "Emma Watson",
        "userUID" : "ASKDJHAIUCHA" 
      }
    }
  },
  "users" : {
    "YYY" : {
      "deviceID" : "1234456",
      "name" : "Andrew Garfield",
      "userUID" : "YYY"
    }
  }
}