I have used JSQMessagesViewController
in my Viewcontroller
. The chat opens with anonymous user. The chat is working fine but i need some change in database. As database is firebase for it. I'm creating a child in my database when any message go to the database it shows , first it show child with the name "Message" and under message all the messages of chat are shown , i want to remove this child , the chat directly should be shown when it goes in database and also should rescieve all the messages directly without interfernce of any child in databse. My code is this for chat,
import UIKit
import Firebase
import FirebaseDatabase
@objc class ChatViewViewController: JSQMessagesViewController, UINavigationControllerDelegate{
var ref: DatabaseReference!
var Name : String?
private var messages = [JSQMessage]()
lazy var messageRef = Database.database().reference().child("Message")
var newMessageRefHandle: DatabaseHandle?
lazy var outgoingBubbleImageView: JSQMessagesBubbleImage = self.setupOutgoingBubble()
lazy var incomingBubbleImageView: JSQMessagesBubbleImage = self.setupIncomingBubble()
private var updatedMessageRefHandle: DatabaseHandle?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Chat Room"
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(addTapped))
self.ref = Database.database().reference(fromURL: "Database URL")
guard let str = UserDefaults.standard.string(forKey: "name") else {
return
}
senderDisplayName = str
self.senderId = Auth.auth().currentUser?.uid as NSString?! as String!
collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSize.zero
collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSize.zero
observeMessages()
// Do any additional setup after loading the view.
}
Send button code is this,
override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) {
let itemRef = messageRef.childByAutoId()
let messageItem = [
"senderId": senderId!,
"senderName": senderDisplayName!,
"text": text!,
]
itemRef.setValue(messageItem)
JSQSystemSoundPlayer.jsq_playMessageSentSound()
collectionView.reloadData()
finishSendingMessage()
}
For showing messages in UI is this,
private func observeMessages() {
//messageRef = channelRef!.child("messages")
// 1.
let messageQuery = messageRef.queryLimited(toLast:25)
// 2. We can use the observe method to listen for new
// messages being written to the Firebase DB
newMessageRefHandle = messageQuery.observe(.childAdded, with: { (snapshot) -> Void in
// 3
let messageData = snapshot.value as! Dictionary<String, String>
if let id = messageData["senderId"] as String!, let name = messageData["senderName"] as String!, let text = messageData["text"] as String!, text.characters.count > 0 {
// 4
self.addMessage(withId: id, name: name, text: text)
// 5
self.finishReceivingMessage()
}
})
updatedMessageRefHandle = messageRef.observe(.childChanged, with: { (snapshot) in
})
}