Retrieve group/muc chat history using smack in android

667 Views Asked by At

I am using the Smack 4.3.4 android library. I am facing issues while fetching the chat history of a particular group using the below code. But its return 0 However, There are many messages which I can see using another client web.

private fun initGroupChatRoom(){
       val mucEnterConfiguration = multiUserChat?.getEnterConfigurationBuilder(nickname)!!
           .requestNoHistory()
           .build()

       try {
           if (!multiUserChat!!.isJoined) {
               multiUserChat?.join(mucEnterConfiguration)
           }
       }catch (e: Exception){
           Log.e("GrpChatAct","Join Error: "+e.toString())
       }

       multiUserChat?.addMessageListener(incomingMessageListener)
   }

private fun chatHistory(){
       CoroutineScope(Dispatchers.IO).launch {
           var mamManager = MamManager.getInstanceFor(multiUserChat)

           var result =  async {
               mamManager.enableMamForAllMessages()
               mamManager.queryMostRecentPage(jId, 70)
           }.await()
        Log.e("GrpChatAct","Total Messages: "+result.messageCount)
     }
}

I also tried with below changes in MamManager:

var mamManager = MamManager.getInstanceFor(mConnection)

Where mConnection is smack connection object.

But still, I am getting 0 messages in the result.

I followed code from this reference.

Is there anything I miss in the code or mistakes I made?

1

There are 1 best solutions below

0
AudioBubble On

fetching the chat history of a particular group using the below code

val multiUserChatManager = MultiUserChatManager.getInstanceFor(XmppChatConnection.instance?.connection)

 val multiUserChat = multiUserChatManager?.getMultiUserChat(mucJid)
                    val mamManager = MamManager.getInstanceFor(multiUserChat)
                    val mamQueryArgs = MamManager.MamQueryArgs.builder()
                            .limitResultsSince(endDate)
                            .setResultPageSize(1000000000)
                            .queryLastPage()
                            .build()
                    val mamQuery = mamManager.queryArchive(mamQueryArgs)
                    val messageList = mamQuery.messages