Vipul
Vipul

Reputation: 41

Read unread message in JSQMessagesViewController using Swift 3

I'm trying this method on button click but not get result:

@IBAction func childAdded(_ sender: Any) {       
    let ref = Constants.refs.databaseChats.childByAutoId()

    ref.observe(DataEventType.childAdded , with: { (snapshot) in
        let msgData = snapshot.value as? NSDictionary
        let msg = msgData?["status"] as? String

        if msg == "sent" {
            let prntRef = Constants.refs.databaseChats.child("chats").child(ref.key)
            prntRef.updateChildValues(["status":"Read"])
            print("Message Read")
        }
    }) { (e) in
        print(e.localizedDescription)
    }
}

Upvotes: 0

Views: 783

Answers (1)

Dan Leonard
Dan Leonard

Reputation: 3395

There are a few ways of doing this and some have advantages over others. One solution would be to store a variable like lasReadVariable or something, in firebase. that is the last read message for user1 then every time the chatView is opened update that lasReadVariable for last read message on this thread. Have an observer for user2 in the conversation on the lasReadVariable and update the messageState accordingly.

Let me know if there is more clarification I can give.

Upvotes: 1

Related Questions