Retrieving offline messages using asmack

I am new to asmack and openfire, looked at the working answer a lot, but could not find it anywhere. How to get offline messages when logging into my asmack account?

I 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);
          }

, , Openfire . ( ). , , , . . . 0 , . mysql db, . , , , . , . - , .

+4
2

, , . , , , , .

- ?

0

afetr XMPP. packetListener, . , .

0

Source: https://habr.com/ru/post/1535035/


All Articles