2

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
                          })
}

This is how my database look enter image description here

siddle
  • 137
  • 2
  • 10

1 Answers1

0

I don’t know if I completely understand what your issue is but from what I can gather you are trying to remove the Message layer from your data base. You should be able to accomplish this by changing this line at the top of your file

lazy var messageRef = Database.database().reference().child("Message") to -> lazy var messageRef = Database.database().reference()

this will remove that layer. Let me know if that is not the issue you are describing.

Dan Leonard
  • 3,325
  • 1
  • 20
  • 32