Firebase CloudFunctions:查询节点 [英] Firebase CloudFunctions: Querying Nodes

查看:120
本文介绍了Firebase CloudFunctions:查询节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过FCM探索云端功能来向我的iOS应用设备发送推送通知。我有以下结构,我正在听取注册事件的人。我想提取eventHost,以便我可以去我的用户节点找到userUID,最终他的deviceID,并发送一个推送通知给他。

  {
events:{
XXX:{
eventHost :YYY,//< - 如何获得这个节点?
eventID:XXX,
registered:{//< - 监听这个节点
ASKDJHAIUCHA:{
name:Emma Watson,
userUID:ASKDJHAIUCHA
}
},
},

},

用户:{
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);
$ b $常量有效载荷= {
通知:{
title: ,
正文:有人注册你的活动!
}
};

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]
让registerPerson = [user.userUID!:personDict as AnyObject] as [String:AnyObject]
let values = [registered:registerPerson ]

FIRHelperClient.sharedInstance.checkIfEventHasRegistrants(ref,event.eventID!){(has,error)in
if error = error {
print(error.localizedDescription)
} else {
如果let has = has {
if have {
self.ref.child(events)。child(self.event.eventID!)。child registered)。updateChildValues(registerPerson)

} else {
self.ref.child(events)。child(self.event.eventID!)。updateChildValues(values)








$ p
$ b

我的云端函数绝对不是完整的,因为目前我正在对设备ID进行硬编码。因为我对Node.js非常不熟悉,并且正在尝试在Swift和服务器端代码中写入iOS代码,所以如果这个问题是基本的,请原谅我。这里的一些建议将是有益的,谢谢。

解决方案

您需要阅读主机,您的代码目前不。
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ export $。$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b .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 );
$ b $常量有效载荷= {
通知:{
标题:事件注册,
正文:有人注册了你的事件!
}
};

const options = {
priority:high
};

return admin.messaging() .sendToDevice(theDeviceID,有效载荷,选项)
});
});

我强烈建议您花一些时间学习如何通过JavaScript与Firebase数据库交互,然后继续。这不一定要通过云功能。您也可以在客户端的Node.js上使用Firebase数据库管理SDK 或者 Firebase codelab for web 。无论您采取哪种方式,都可以确保您准备好更好地通过云端函数与数据库进行交互。

作为最后的警告:您嵌套了多种数据类型在一个单一的名单。这是不推荐的,因为它会导致各种各样的问题。相反,我会把注册用户拉到他们自己的顶级节点,这样你就可以得到:

  {
events:{
XXX:{
eventHost:YYY,
eventID:XXX
}
},
registered:{
XXX:{
ASKDJHAIUCHA:{
name:Emma Watson,
userUID:ASKDJHAIUCHA


$ b $ users
$ Y $
deviceID:1234456,
名称:Andrew Garfield,
userUID:YYY
}
}
}


I am exploring Cloud Functions with FCM to do a push notification to my iOS app device. I have the following structure and I am listening for people who registered to an event. I want to extract the eventHost so that I can go to my users node to find the userUID and eventually his deviceID, and send a push notification to him.

{
  "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"
    },
  }
}

My code for the Cloud Functions as such so far:

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)

    });

And my Swift portion when adding value into the database is as such:

@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)

                    }
                }
            }
        }
    }

My cloud functions is definitely not complete as currently I am hardcoding theDeviceID. As I am pretty inexperience with Node.js and am trying to write both the iOS code in Swift and the server side code, so pls pardon me if this question is elementary. Some advice here would be helpful, thanks.

解决方案

You'll need to read the host, which your code currently doesn't do.

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)
      });
    });

I highly recommend that you spend some time learning how to interact with the Firebase Database through JavaScript before continuing though. This doesn't have to be through Cloud Functions. You could also use the Firebase Database Admin SDK from Node.js on your client or take the Firebase codelab for web. Whichever one you take, it will ensure you're much better prepared for interacting with the database through Cloud Functions.

As a final warning: you're nesting multiple data types under a single list. This is not recommended, since it leads to all kinds of problems down the line. Instead, I would pull the registered users into their own top-level node, so that you get:

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

这篇关于Firebase CloudFunctions:查询节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆