3
votes

I am trying a chat application with openfire,smack and android where offline messages are not working. if both the users are online, able to send and receive the messages correctly . But if user A is offline and user B sends a message, User A is not getting the sent message of B once he is online.Tried possible solutions from stackoverflow but none of them working. using the below code to retrieve the offline messages.

new Thread(){ public void run(){

            XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()

                    .setSocketFactory(SocketFactory.getDefault())

                    .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)

                    .setServiceName("123.456.0.854")//service name

                    .setHost("123.456.0.854") // host name 

                    .setPort(5222) //port

                    .setUsernameAndPassword("phone", "admin")
                    .setConnectTimeout(40000)

                    .setCompressionEnabled(false).build();

            connection = new XMPPTCPConnection(config);

            try {
                connection.connect();
                connection.login("phone", "admin");
                Presence presence = new Presence(Presence.Type.available);
                presence.setStatus("Available");
                try {
                    connection.sendStanza(presence);
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }
            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XMPPException e) {
                e.printStackTrace();
            }



            OfflineMessageManager offlineMessageManager = new OfflineMessageManager(connection);

            try {
                System.out.println("Count is " +offlineMessageManager.getMessageCount());
            } catch (SmackException.NoResponseException e) {
                e.printStackTrace();
            } catch (XMPPException.XMPPErrorException e) {
                e.printStackTrace();
            } catch (SmackException.NotConnectedException e) {
                e.printStackTrace();
            }

        }
    }.start(); 
1

1 Answers

0
votes

After lot struggle, I have resolved the issue. In your openfire admin page, go to "client settings" and reduce the idle time from 360sec (by default) to 1 sec(may be). Only then when you disconnected from Internet, it can detect that you are offline and preserve rest of the messages as OFFLINE.

@Override public void onNetworkConnectionChanged(boolean isConnected) {

if(isConnected){
    new Thread() {

        public void run() {
            try {
                XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
                builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
                builder.setUsernameAndPassword("phone", "admin");
                builder.setSendPresence(true);
                builder.setServiceName(<Service name>);
                builder.setHost(<Host name>);
                builder.setResource("Test");
                builder.setDebuggerEnabled(true);
                Presence presence = new Presence(Presence.Type.available);
                presence.setStatus("Available");
                connection = new XMPPTCPConnection(builder.build());
                connection.connect();
                connection.login();
                Presence presence123 = new Presence(Presence.Type.available);
                presence123.setStatus("Available");
                try {
                    connection.sendStanza(presence123);
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }
                StanzaFilter filter = new AndFilter(new StanzaTypeFilter(Message.class));
                PacketListener myListener = new PacketListener()
                {
                    public void processPacket(Stanza stanza)
                    {
                        retrieveMessage(stanza,userType);
                    }
                };
                connection.addPacketListener(myListener, filter);
                try {
                    connection.sendStanza(presence);
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }

            } catch (SmackException | XMPPException | IOException e) {
                e.printStackTrace();
            }


            //return connection.isConnected();
        }

    }.start(); 

The above is working fine and able to retrieve the offline messages. The method "retrieveMessage(stanza,userType);" is used to process the incoming message and update the Adapter. Make sure to send the Presence as "Available" when you reconnect. Please let me know if there are still any issues.