1

i'm using JSQMessages View Controller third party lib in my app for firebase chat. As following some video and documents i have used all the delegates and datasource methods in my class and name my subclass as JSQMessagesViewController. But when i hit send button i reload the collection view to show messages in collection view but they aren't showing the messages. When i hit send button it send message to firebase database but does not show my message above in collection view. I have commented the button method also and use break points also but nothing is working. How can i show my send messages above in collection view. My code is this,

import UIKit
import Firebase
import FirebaseDatabase
class ChatViewViewController: JSQMessagesViewController {
private var messages = [JSQMessage]()
override func viewDidLoad() {
    super.viewDidLoad()
self.senderId = "1"
    senderDisplayName="Hamza"
collectionView.reloadData()
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return messages.count
}
 override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
    return messages[indexPath.item]
}
 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell
 return cell
}
 override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
    return nil
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! {
let bubbleFactory = JSQMessagesBubbleImageFactory();

    return bubbleFactory?.outgoingMessagesBubbleImage(with:UIColor.blue);
}
override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) {
    messages.append(JSQMessage (senderId: senderId, displayName: senderDisplayName, text: text))
    print("Helllo")
    collectionView.reloadData()

    finishSendingMessage()
}
siddle
  • 137
  • 2
  • 10

1 Answers1

0

Use this function for display a message .

 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()
 } else {
 print("Error! Could not decode message data")
}
})
}

Next, call your new method in viewDidLoad():

observeMessages()
tabassum
  • 1,100
  • 6
  • 17