Java – One-on-one chat history with open fire and smack

One-on-one chat history with open fire and smack… here is a solution to the problem.

One-on-one chat history with open fire and smack

I

have installed Open Fire on my system and by using the postman tool I am able to create users, and by using smack I am also able to send messages to other users: But the problem is that I don’t know how to get chat history between two users: This means that if I send the sender and sender username, I need to get the previous chat history.
I can see the chat history in the open fire servers -> archieving folder.
But I don’t know how to get chat history.
Are there any Rest APIs that can be used to get chat history between two users

Please provide any possible solutions

Thanks

This is the chat history I can see

enter image description here

Solution

If you want to use smack to get chat history from Openfire:

  1. As you’ve already done, install MonitoringService Enable the plug-in in MAM (XEP-0313) Openfire.

  2. Now from the Openfire server, go to: Server> Archive > Archive Settings and check “Archive one-to-one chats” and “Archive group chats” and save click “Update Settings”.

  3. From now on, all chat history will be saved on Openfire. Start a new chat with someone and reinstall your Android app.

  4. MAM is “smack-experimental “. So you have to add this line to your Gradle:

    implementation 'org.igniterealtime.smack:smack-extensions:4.2.2'
    
  5. Once you have successfully connected and authorized one of them, you can get the chat history page by page or as needed by the following code:

    MamManager manager = MamManager.getInstanceFor(connection);
    MamManager.MamQueryResult r = manager.mostRecentPage([userBareJID], [numberOfMessages]);
    if (r.forwardedMessages.size() >= 1) //printing first of them
    {
        Message message = (Message) r.forwardedMessages.get(0).getForwardedStanza();
        Log.i("mam", "message received" + message.getBody());
    }
    

Related Problems and Solutions