简体   繁体   中英

Check how many messages have been sent

I am making a messenger app, I am using firebase as my database, and I am using MVVM pattern.

I want to check if the user have new unread messages and display the number of messages near the person who sent it.

So in the MainActivity there is a recyclerView filled with users. This is the user model( https://ibb.co/w72K17m ).

I have a "seen" key,which means the user haven't read the message yet,in addition, I also have "reciever" which contains the current user UID. With those values I tried to do the followings:

chatRef.addValueEventListener(object : ValueEventListener {
    override fun onDataChange(snapshot: DataSnapshot) {

        val messageArray = ArrayList<String?>()
        val usersArray = ArrayList<String?>()

        for (snapshot2 in snapshot.children) {

            val chat = snapshot2.getValue(Message::class.java) // Getting all the messages with values, like the photo.

            if (chat!!.receiver.equals(currentUser.value!!.uid)) { // Check if the current user is the reciever
                if (chat.seen == false){ // Check if the message is seen

                    messageArray.add(chat.message)
                    usersArray.add(chat.sender)
                }
            }
        }

        Log.e("Chat","messageArray: $messageArray")
        Log.e("Chat","usersArray: $usersArray")
    }

    override fun onCancelled(error: DatabaseError) {
        TODO("Not yet implemented")
    }
})

This is the output:

messageArray: [message1, message2 , message3, message4, message5 , message6]

usersArray: [4RDojKDSJZUOfpNky9MxuLHQJN63, 4RDojKDSJZUOfpNky9MxuLHQJN63, iromTJzrZCQVJnjcLhhdUjXi5bP2, iromTJzrZCQVJnjcLhhdUjXi5bP2, iromTJzrZCQVJnjcLhhdUjXi5bP2, iromTJzrZCQVJnjcLhhdUjXi5bP2]

I am getting all the messages and all the users who sent it to me,but I don't know how to check how many messages each ID sent.

I tried using hashMap but with no success..

Thank you and bless you all!

I used HashMap after all, this is what I did:

fun searchNumberOfMessages(user: User){

    val hasMap = HashMap<String, Any?>()
    
    hasMap.clear()
    hashMapArray.clear()

    if (newMessages.value != null){
        (newMessages.value as ArrayList<HashMap<String, Any?>>).clear()
    }

    chatRef.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {

            val messageArray = ArrayList<String?>()

            for (snapshot2 in snapshot.children) {

                val chat = snapshot2.getValue(Message::class.java)

                if (chat!!.receiver.equals(currentUser!!.uid) && chat.sender.equals(
                        user.uid)) {

                    if (chat.seen == false){

                        messageArray.add(chat.message)

                    }

                }

            }

            hasMap["Sender"] = user
            hasMap["Messages"] = messageArray.size
            hashMapArray.add(hasMap)
            newMessages.value = hashMapArray

        }

        override fun onCancelled(error: DatabaseError) {
            TODO("Not yet implemented")
        }
    })

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM