Reputation: 317
I have tried many solutions to make this work, but it doesn't seem to work with me. These are the solutions I have tried:
Firebase Retrieving Data in Swift
https://firebase.google.com/docs/database/ios/read-and-write
https://www.raywenderlich.com/187417/firebase-tutorial-getting-started-3
I am trying to retrieve the deviceToken of the currently logged in user, for example if John logs in, it would assign or retrieve his deviceToken and assign it to a variable.
The closest I have got was with this code, but I get every profile with every data stored in that profile instead of the currently logged in one.
let userID = Auth.auth().currentUser?.uid
let ref = Database.database().reference(withPath: "users/profile/\(userID)")
ref.observeSingleEvent(of: .value, with: { snapshot in
if !snapshot.exists() {
return
}
let token = snapshot.childSnapshot(forPath: "deviceToken").value
print(token!)
})
Upvotes: 0
Views: 293
Reputation: 1791
There is an example on the documentation, modifying for your case:
let userID = Auth.auth().currentUser?.uid
ref.child("users").child("profile").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let deviceToken = value?["deviceToken"] as? Int ?? ""
// ...
}) { (error) in
print(error.localizedDescription)
}
Upvotes: 1