Retrieving offline messages using asmack

2019-04-10 05:06发布

问题:

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.

回答1:

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?



回答2:

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.