Abhishek V
Abhishek V

Reputation: 12536

Displaying list of contacts/friends with previous chat history in smack

I am able to fetch the archived messages of a one-to-one chat by using MamManager class provided by Smack (doc). But I have a requirement to display list of users along with latest message in a list similar to what whats app does.

enter image description here

Does anyone have any idea about how I can achieve this using smack & openfire? Any help would be appreciated. Thanks

Upvotes: 2

Views: 1008

Answers (1)

Sarthak Mittal
Sarthak Mittal

Reputation: 6114

Use Roster class for fetching contacts in Smack. Also persist those contacts in your local database for offline support.

For implementing Whatsapp like contact import. You will have to fetch the contacts from your device and add them to your Roster. An ideal way of doing this would be to send the contacts via Rest-API and let the server add them to your Roster.

Once you initialize your connection object, you can attach a roster listener like so:

val connection:XMPPTCPConnection // assuming you have the connection object
val roster = Roster.getInstanceFor(connection)
// NOTE: You should attach your roster listener even before calling connect() on your connection object.
// This way you get the roster list whenever the connection connects!
roster.addRosterLoadedListener(object:RosterLoadedListener{
    override fun onRosterLoaded(roster: Roster?) {
        // Update your database
    }
    override fun onRosterLoadingFailed(exception: java.lang.Exception?) {
        // Handle error
    }
})

For persisting messages you will have to use local database(SQLite or some ORM)

Upvotes: 3

Related Questions