2

I have successfully implemented one on one chat in my Android application. I have used SQLite to save the chat between two users as in app database. But this chat is removed when the user uninstalls the application. What i need to know is how i can save the chat between two users on my server so whenever the user reinstalls the app or logs in any other device he can see his previous chat. I have enable mod_archive and mod_mam on my ejabberd server and i am able to retrieve offline messages in the app. Thanks

1 Answers1

5

You need to follow xep - 0136

http://xmpp.org/extensions/xep-0136.html

For smack you need to get it in following way :

  public void loadArchiveMessages(Jid jid, XMPPTCPConnection xmppTcpConnection){
        try {
            MamManager mamManager=MamManager.getInstanceFor(xmppTcpConnection);
            MamManager.MamQueryResult mamQueryResult = mamManager.queryArchive(jid);
            List<Forwarded> forwardedMessages=mamQueryResult.forwardedMessages;
            Iterator<Forwarded> forwardedIterator=forwardedMessages.iterator();
            while (forwardedIterator.hasNext()){
                Forwarded forwarded=forwardedIterator.next();
                Stanza stanza=forwarded.getForwardedStanza();
                if (stanza instanceof Message) {
                    String messageId=stanza.getStanzaId();
                    xmppTcpConnection.processMessage((Message) stanza);
                }
            }
        } catch (XMPPException.XMPPErrorException e) {
            e.printStackTrace();
        } catch (SmackException.NotLoggedInException e) {
            e.printStackTrace();
        } catch (SmackException.NotConnectedException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (SmackException.NoResponseException e) {
            e.printStackTrace();
        }
    }
Jaspreet Chhabra
  • 1,431
  • 15
  • 23
  • Hi Jaspreet !! Thanks for your answer. I tried to implement this code in my app but i am unable to resolve MamManager and Jid in Android Studio. And may i know the strings you have used in Forwarded class ? Please let me know if i am missing anything. – Paritosh Chaudhary Dec 26 '16 at 05:42
  • package org.jivesoftware.smackx.mam; UPVOTE PLEASE – Jaspreet Chhabra Dec 27 '16 at 16:58
  • 1
    Hi Jaspreet..!! Thanks again for your reply. I can't find this package in my imported libraries. Sorry if i am asking any silly question here but i am new to this smack implementation. Is there any jar file or updated version of smack library for this. I am presently using 4.1.9 version. When i searched a little more about retrieving chat history from server i came across a new process i.e. sending custom IQ to server. Can you please explain me how your method is different from the custom IQ part.Little more guidance in this process will really be appreciated. Thanks in advance. – Paritosh Chaudhary Dec 28 '16 at 10:00
  • 1
    I am using : compile "org.igniterealtime.smack:smack-im:$smackVersion" compile "org.igniterealtime.smack:smack-android:$smackVersion" compile "org.igniterealtime.smack:smack-tcp:$smackVersion" compile "org.igniterealtime.smack:smack-experimental:$smackVersion" compile "org.igniterealtime.smack:smack-android-extensions:$smackVersion" – Jaspreet Chhabra Dec 28 '16 at 15:48
  • Hello Jaspreet...!! Now i can resolve all other methods except xmppTcpConnection.processMessage((Message) stanza); Is there any other method for resolving this? Thanks – Paritosh Chaudhary Dec 30 '16 at 09:05
  • Hi Jaspreet..!! Thanks for helping. I have accepted the answer as the correct solutions for fetching the chat history. Now I am now able to retrieve the chat history using the above method. I also wanted to know if we can fetch the last message in one to one chat to show in the friends list and what about the timestamp when we fetch the chat history> How we can fetch that. – Paritosh Chaudhary Jan 03 '17 at 06:37
  • You can do that with last message id – Jaspreet Chhabra Jan 04 '17 at 09:59