JSQMessagesViewController 和 Firebase

JSQMessagesViewController and Firebase

我正在使用 FirebaseJSQMessagesViewcontroller。我添加了一些消息进行测试,但没有出现。谁能帮忙看看我错过了什么?谢谢!

我的代码:

import UIKit
import Firebase
import JSQMessagesViewController

class ChatViewController: JSQMessagesViewController {

var messages = [JSQMessage]()

lazy var outgoingBubbleImageView:JSQMessagesBubbleImage = self.setupOutgoingBubble()
lazy var incomingBubbleimageView:JSQMessagesBubbleImage = self.setupIncomingBubble()


override func viewDidLoad() {
    super.viewDidLoad()

    self.senderDisplayName = "cindy"
    self.senderId = FIRAuth.auth()?.currentUser?.uid

    print("sender id is: \(self.senderId)")
}

override func viewDidAppear(_ animated: Bool) {

    addMessage(withId: "john", name: "doo", text: "hello, I am here")

    addMessage(withId: senderId, name: "Me", text: "how are you")

    addMessage(withId: senderId, name: "Me", text: "i am earlier than you.")

   // I added finishReceivingMessage() here, but the app crashed.

}

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: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! {

    let message = messages[indexPath.item]

    if message.senderId == senderId {

        return outgoingBubbleImageView
    } else {

        return incomingBubbleimageView
    }
}

func addMessage(withId id:String, name: String, text:String) {

    if let message = JSQMessage(senderId: id, displayName: name, text: text) {

        self.messages.append(message)

    }

}

func setupOutgoingBubble() -> JSQMessagesBubbleImage {

    let bubbleImagesFactory = JSQMessagesBubbleImageFactory()

    return (bubbleImagesFactory?.outgoingMessagesBubbleImage(with: UIColor.jsq_messageBubbleBlue()))!

}

func setupIncomingBubble() -> JSQMessagesBubbleImage {

    let bubbleImagesFactory = JSQMessagesBubbleImageFactory()

    return (bubbleImagesFactory?.incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleLightGray()))!
 }

}

您必须在添加所有示例消息后添加 finishReceivingMessage()。这有点像 tableView.reloadData(),除了 JSQMessagesViewController。它还会为您滚动到最新消息。

因此,对于初学者,我建议您去查看 JSQMessagesVeiwControllerDevelop 分支,并提取其中包含的 SwiftExample。这是最新版本,已针对 swift3 进行了更新。然后我会隔离你的问题。由于您实际上是在本地制作消息而不是利用 firebase 我会评论它。

然后我们可以像他们在 DemoConversation.swift 文件

中那样进行虚假对话
let message = JSQMessage(senderId: "john", displayName: "doo", text: "hello, I am here")
let message2 = JSQMessage(senderId: "asdf", displayName: "Me", text: "how are you")
let message3 = JSQMessage(senderId: "asdf", displayName: "Me", text: "i am earlier than you.")


func makeNormalConversation() -> [JSQMessage] {
  conversation = [message, message2, message3]
  return conversation
}

然后您只需要确保您已设置好所有内容供您查看并将您的消息设置为 makeNormalConversation()

那么对于您的 ChatViewController,最少的代码量是这样的:

//
//  ChatViewController.swift
//  SwiftExample
//
//  Created by Dan Leonard on 5/11/16.
//  Copyright © 2016 MacMeDan. All rights reserved.
//

import UIKit
import JSQMessagesViewController

class ChatViewController: JSQMessagesViewController {
  var messages = [JSQMessage]()
  let defaults = UserDefaults.standard
  var conversation: Conversation = makeConversation() // Here is where your messages are added.
  var incomingBubble: JSQMessagesBubbleImage!
  var outgoingBubble: JSQMessagesBubbleImage!
  fileprivate var displayName: String!

  override func viewDidLoad() {
    super.viewDidLoad()
        incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage(with: UIColor.jsq_messageBubbleBlue())
        outgoingBubble = JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImage(with: UIColor.lightGray)

        collectionView?.collectionViewLayout.incomingAvatarViewSize = .zero
        collectionView?.collectionViewLayout.outgoingAvatarViewSize = .zero

    automaticallyScrollsToMostRecentMessage = true

    self.collectionView?.reloadData()
    self.collectionView?.layoutIfNeeded()
  }


  // MARK: JSQMessagesViewController method overrides
  override func didPressSend(_ button: UIButton, withMessageText text: String, senderId: String, senderDisplayName: String, date: Date) {
    let message = JSQMessage(senderId: senderId, senderDisplayName: senderDisplayName, date: date, text: text)
    self.messages.append(message)
    self.finishSendingMessage(animated: true)
  }

  // MARK: JSQMessages CollectionView DataSource

  override func senderId() -> String {
    return "Cindy"
  }

  override func senderDisplayName() -> String {
    return "Cindy Lue"
  }

  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: JSQMessagesCollectionView, messageBubbleImageDataForItemAt indexPath: IndexPath) -> JSQMessageBubbleImageDataSource {

    return messages[indexPath.item].senderId == self.senderId() ? outgoingBubble : incomingBubble
  }

}

查看 develop 分支上的示例项目以了解更多详细信息,如果您有更多问题,请告诉我?

然后从这一点开始你应该添加 Firebase。您在 senderID 的正确路径上只是确保您实际上为已登录的用户获得了唯一的 ID,然后确保它与该用户发送的消息的 ID 相同。

祝你好运