1

I am trying to retrieve a persons user information such as, first and last name from the firebase database and have it displayed above the message bubble when a user sends a message to another user.

Below is my code. I am able to send send messages, it is just displaying the firebase user name. Any suggestions?

Thank you!

class ChatViewController: JSQMessagesViewController {

var messages = [JSQMessage] ()

lazy var outgoingBubble: JSQMessagesBubbleImage = {
    return JSQMessagesBubbleImageFactory()!.incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleLightGray())
} ()

lazy var incomingBubble: JSQMessagesBubbleImage = {
    return JSQMessagesBubbleImageFactory()!.incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleBlue())
} ()

override func viewDidLoad() {
    super.viewDidLoad()

    self.senderDisplayName = "Wolverine"
    self.senderId = Auth.auth().currentUser?.uid


    // The first line hides the attachment button on the left of the chat text input field. The other two lines of code set the avatar size to zero, again, hiding it.

    inputToolbar.contentView.leftBarButtonItem = nil
    collectionView.collectionViewLayout.incomingAvatarViewSize = CGSize.zero
    collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSize.zero

    // Do any additional setup after loading the view.
    let query = Constants.refs.databaseChats.queryLimited(toLast: 10)

    _ = query.observe(.childAdded, with: { [weak self] snapshot in

        if  let data        = snapshot.value as? [String: String],
            let id          = data["sender_id"],
            let name        = data["name"],
            let text        = data["text"],
            !text.isEmpty
        {
            if let message = JSQMessage(senderId: id, displayName: name, text: text)
            {
                self?.messages.append(message)

                self?.finishReceivingMessage()
            }
        }
    })
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
    return messages[indexPath.item]

}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return messages.count
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource!
{
    return messages[indexPath.item].senderId == senderId ? outgoingBubble : incomingBubble
}

// This function simply returns nil when JSQMVC wants avatar image data, effectively hiding the avatars. hides the avatars for message bubbles. JSQMVC can show them, but its not needed.

override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource!
{
    return nil
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, attributedTextForMessageBubbleTopLabelAt indexPath: IndexPath!) -> NSAttributedString!
{
    return messages[indexPath.item].senderId == senderId ? nil : NSAttributedString(string: messages[indexPath.item].senderDisplayName)
}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAt indexPath: IndexPath!) -> CGFloat
{
    return messages[indexPath.item].senderId == senderId ? 0 : 15
}
override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!)
{
    let ref = Constants.refs.databaseChats.childByAutoId()

    let message = ["sender_id": senderId, "name": senderDisplayName, "text": text]

    ref.setValue(message)

    finishSendingMessage()
  }
 }
AP.fix
  • 37
  • 1
  • 7
  • Why are you performing a query when you already know who the user is? The flow should be: the user authenticates, you then have their uid, then read a /users/uid node to capture their username etc from Firebase (using .observeSingleEvent) and when you send a message, send that username with it. Maybe I don't understand the question. – Jay Dec 28 '18 at 20:01

0 Answers0