gpt4 book ai didi

ios - Firebase 事件监听器不起作用,我无法收到正确的快照

转载 作者:行者123 更新时间:2023-11-29 11:45:31 25 4
gpt4 key购买 nike

我是新来的,如果我没能创造出好的东西请见谅
和格式正确的问题。

我的问题是我无法在 Firebase 中获取特定子项的快照。

我需要放置一个事件监听器,它会在数据库发生变化时返回值(用户的 token ):

notifications.child("tokens").child("id_user_token").child(user)

我还张贴了一张数据库树的图片,这样你就可以看到里面有什么。我的目的是保存从数据库中检索到的 token 并创建一个 HashMap(以构建通知),其中将包含 token 、标题和消息。从代码中可以看出,我使用 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)

}

最佳答案

尝试如下更改代码

import FirebaseDatabase

class FirebaseMessagingService{

var ref: DatabaseReference!

init(user: String, title: String, message: String) {
self.ref = Database.database().reference()

let notifRef = self.ref.child("notificationRequests")
let thisUserRef = notifRef.child("tokens").child("id_user_token").child(user)
print(thisUserRef)
thisUserRef.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot)
})
}
}

至少应该打印 thisUserRef 应该是

yourDataBase/notificationRequests/tokens/id_user_token/2

它还应该打印快照

snap( 2: cq3Xy...... )

让我们知道打印的内容以及打印时路径是否正确。

此外,在您的问题中您陈述了您的目标

I need to put an event listener which will return me the value( the token of the user) when there is a change in the database:

您提供的代码不会这样做。你正在使用

.observeSingleEvent

它就是这样做的 - 一次观察一个事件。它不会在节点上为 future 的事件留下一个监听器。

如果你想观察所有事件然后使用

   .observe(.value... (or .childAdded etc)

但是,我认为您的意思并不是要观察该特定节点的变化(或者您可能这样做了)。您的意思可能是,当其他地方的数据库发生变化时,您将需要获取该用户的 uid,您问题中的代码正试图这样做。

最后一件事:

您真的不应该将其定义为类。它实际上应该是一个在您的 viewController 中这样调用的函数

class ViewController: UIViewController {

var ref: DatabaseReference!

override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
self.showUid(user: "2", title: "some title", message: "some message")
}

func showUid( user: String, title: String, message: String) {
let notifRef = self.ref.child("notificationRequests")
let thisUserRef = notifRef.child("tokens").child("id_user_token").child(user)
print(thisUserRef)
thisUserRef.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot)
})
}
}

关于ios - Firebase 事件监听器不起作用,我无法收到正确的快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44112139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com