I want to get title and body from my Firestore data structure, data structure shown in the picture below. I tried this code, with this I got complete data but I want title and body individually.

        let docRef = db.collection("test").document("test")

    docRef.getDocument { (document, error) in
        if let notidata = document, notidata.exists {
            let notiDetail = notidata.data().map(String.init(describing:)) ?? "nil"
            print("Document data: \(notiDetail)")
        } else {
            print("Document does not exist")
        }
    }

Kindly help me out with that.

enter image description here

1 Answers

0
wings On

Try this code:-

docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            print(document.data()!)
            let userNotifications = document.data()["userNotifications"] as? [[String:Any]]
            for notificaton in userNotifications  {
            let body = notificaton["body"] as? String ?? ""
            let title = notificaton["title"] as? String ?? ""
            print(body, title)
         }

        } else {
            print("Document does not exist")
        }
    }