2

I have a chat done in firestore and obviously I need to get the messages from each chat room but the messages never come sorted by the date always by the id of the user.

In the beginning I did without using @ServerTimestamp and was generating the date on the Android device itself but at the suggestion right here in Stackoverflow I changed to the firestore itself to generate the date, according to the suggestion that would solve but it does not work. It's coming in an order I did not order, it's coming in the order of the user id, lateinit var from_id: String

class Message {

    lateinit var content: String

    @ServerTimestamp
    var timestamp: Timestamp ?= null

    lateinit var from_id: String

    lateinit var type: String

    constructor(content: String, timestamp : Timestamp, from_id: String, type: String) {
        this.content = content
        this.from_id = from_id
        this.timestamp = timestamp
        this.type = type
    }

    constructor(content: String, from_id: String, type: String) {
        this.content = content
        this.from_id = from_id
        this.type = type
    }

    constructor()
}

ControllerChat

...

   override fun sendMessageText(idChatRoom: String, from_id: String, text: String, listenerSendMessage: ListenerSendChatMessage) {
        var message = Message(text, from_id, Message.TEXT)

        chatChannelsCollectionRef
                .document(idChatRoom)
                .collection(MESSAGES)
                .add(message).addOnSuccessListener {
                    listenerSendMessage.onSendChatMessageSucess()
                }.addOnFailureListener {
                    listenerSendMessage.errorSendChatMessage(it)
                }
    }

...


override fun getAllMessageFromChatRoom(idChatRoom: String, listenerGetAllChatMessage: ListenerGetAllChatMessage) {
        Log.d(TAG, "ChatController - getAllMessageFromChatRoom")

        listenerSnapshotAllMessageFromChatRoom = chatChannelsCollectionRef

                .document(idChatRoom)
                .collection(MESSAGES)
                .orderBy("timestamp", Query.Direction.ASCENDING)
                .addSnapshotListener(object : EventListener<QuerySnapshot> {
                    override fun onEvent(querySnapshot: QuerySnapshot?, p1: FirebaseFirestoreException?) {
                        querySnapshot?.let { qSnap ->
                            if (!qSnap.isEmpty) {
                                var documentChange = qSnap.documentChanges

                                for (doc in documentChange) {
                                    var message = doc.document.toObject(Message::class.java)
                                    Log.i(TAG, "Document Change: " + message.content)
                                    listenerGetAllChatMessage.onChatMessage(message)
                                }
                            }
                        }
                    }
                })
    }

enter image description here

enter image description here

Hugo
  • 172
  • 2
  • 9
  • Please add your database structure as a screenshot. – Alex Mamo Nov 28 '18 at 13:00
  • Okay, I put the screen print – Hugo Nov 28 '18 at 13:06
  • Have you tried to remove the `timestamp` from the constructor, as explained **[here](https://stackoverflow.com/questions/48474957/servertimestamp-is-allways-null-on-firebase-firestore/48475027)**? Does it work this way? – Alex Mamo Nov 28 '18 at 13:17
  • If you are also interested, I have a tutorial on how to can create a complete and functional [Chat App with Kotlin](https://www.youtube.com/playlist?list=PLn2n4GESV0Ak1HiH0tTPTJXsOEy-5v9qb). – Alex Mamo Nov 28 '18 at 13:18
  • When I send the message I am using the constructor without the timestamp definition. Has more than one constructor defined in my message class – Hugo Nov 28 '18 at 13:48
  • But when are you using the other constructor? What timestamp are you passing to that constructor? – Alex Mamo Nov 28 '18 at 13:54

0 Answers0