4
votes

I'm new to asmack and openfire, looked a lot for a working answer to this but couldn't find it anywhere. How do I retrieve offline messages on Logging into my account on asmack?

I've used the following code:

      configure(ProviderManager.getInstance()); //configuring providers before creating a connection

            ConnectionConfiguration connConfig =  new ConnectionConfiguration(HOST, PORT);

            connConfig.setSendPresence(false);

      connection =  new  XMPPConnection (connConfig); 


     try {
       connection.connect();


     } catch (XMPPException ex) {

         setConnection(null);
     }
      try {


        connection.login(username, password);


        try { 
                  OfflineMessageManager offlineManager = new OfflineMessageManager( 
                    connection);
            Iterator<org.jivesoftware.smack.packet.Message> it = offlineManager 
                    .getMessages(); 
            System.out.println(offlineManager.supportsFlexibleRetrieval()); 
            System.out.println("Number of offline messages:: " + offlineManager.getMessageCount()); 
            Map<String,ArrayList<Message>> offlineMsgs = new HashMap<String,ArrayList<Message>>();   
            while (it.hasNext()) { 
                org.jivesoftware.smack.packet.Message message = it.next(); 
                System.out 
                        .println("receive offline messages, the Received from [" + message.getFrom() 
                                + "] the message:" + message.getBody()); 
                String fromUser = message.getFrom().split("/")[0]; 

                if(offlineMsgs.containsKey(fromUser)) 
                { 
                    offlineMsgs.get(fromUser).add(message); 
                }else{ 
                    ArrayList<Message> temp = new ArrayList<Message>(); 
                    temp.add(message); 
                    offlineMsgs.put(fromUser, temp); 
                } 
            } 
            // Deal with a collection of offline messages ... 

            offlineManager.deleteMessages(); 
        } catch (Exception e) { 
                  Log.e("CATCH","OFFLINE");
            e.printStackTrace(); 
        }

        Presence presence = new Presence(Presence.Type.available);

        connection.sendPacket(presence);
        setConnection(connection);//Packet Listener






        // Set the status to available



          } catch (XMPPException ex) {

            setConnection(null);
          }

From what I've read, once a connection is established, Openfire automatically sends offline messages to the user. (if any) Which means that by just setting packet listeners after logging in, I should be able to retrieve the messages. However, this didn't work for me. Which's why I tried using OfflineMessageManager. It always shows 0 messages as the message count though. I even logged in to the mysql db which the server's using and checked the offline messages folder. The messages exist till the user logs in, which means that the messages are being sent but it isn't being retrieved by the app. I can't seem to find out how implement this. If anyone has a working solution, It'd be greatly appreciated.

2
My bad, I was logging in my login activity first and then sending the username and password to the main chat activity and logging in again. The messages were being sent to the first activity which didn't have a packet listener set before login. I set the packet listener before login there and I can see all the offline messages just fine.user3498348
Thanks fella. Seems to be a silly solution, though Works perfectly.The Heist
@user3498348 Have you able to retrive offline messages? I used to do the same thing what you 've done over your question but I m unable to retrive offline messages from database. can you help meDhiren Hamal
Hi @DhirenHamal You just need to call login after adding packetListener.NehaK

2 Answers

0
votes

i partially solved added a packet listener after login but the same problem persist when internet go down. in that case i intercept reconnectionSuccessful event and remove and add again the packet listener but the messages sent when the user was offline is lost .

Somebody have the best solution to solve it?

0
votes

Send your presence afetr logging in to the XMPP server. You have forgot to add packetListener which listens for the upcoming offline messages. Hope this works.