【发布时间】:2017-05-22 11:50:22
【问题描述】:
我是新来的,如果我没有设法创造一个好的,请原谅我
和格式良好的问题。
我的问题是我无法在Firebase 中获取特定孩子的快照。
我需要放置一个事件监听器,当数据库发生变化时,它将返回值(用户的令牌):
notifications.child("tokens").child("id_user_token").child(user)。
我还发布了数据库树的图像,以便您查看其中的内容。
我的目的是保存从数据库中检索到的令牌并创建一个HashMap(用于构建通知),其中将包含令牌、标题和消息。从代码中可以看出,我使用init() 函数传递了所有参数。我尝试在ViewController.swift 中调用此函数,如下所示:
"FirebaseMessagingService.init(user: "2", title: "Test",message: "Test")"
正如您从数据库图像中看到的那样,有id = 2 的用户,但是当我尝试打印快照时,它会打印null,我无法理解代码有什么问题。Image of the Database tree
Here is the exported JSON
import Foundation
import FirebaseMessaging
import FirebaseDatabase
class FirebaseMessagingService{
var mDatabase : DatabaseReference
var notifications = DatabaseReference()
init(user: String,title: String, message:String){
self.mDatabase = Database.database().reference()
self.notifications = Database.database().reference()
notifications = mDatabase.child("notificationRequests")
var notification = NSMapTable<AnyObject, AnyObject>()
notifications.child("tokens").child("id_user_token").child(user).observeSingleEven t(of: .value, with: { snapshot in
var token = Messaging.messaging().fcmToken! as String
print("\n\n\n\n\(snapshot)")
token = (snapshot.value as? String)!
notification.setValue(token, forKey: "toUser")
notification.setValue(title, forKey: "title")
notification.setValue(message, forKey: "message")
}){ (error) in
print("\n\n\n\n")
print(error.localizedDescription)
}
【问题讨论】:
标签: ios firebase swift3 notifications firebase-realtime-database