powered by Jive Software

smack cannot retrieve offline messages via OfflineMessageManager


#1

I have a problem with while getting offline messages from ejabberd server 18.01 via smack library.
I have a connector method

private static AbstractXMPPConnection connectToServer(String serverHost, Integer port, String userName, String userPassword) {
    
            DomainBareJid xmppDomain = null;
            try {
                xmppDomain = JidCreate.domainBareFrom(serverHost);
            } catch (XmppStringprepException e) {
                System.out.println("XMPP Domain initialization exception " + e.getMessage());
                return null;
            }
    
            XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                    .setXmppDomain(xmppDomain)
                    .setHost(serverHost)
                    .setPort(port)
                    .setDebuggerEnabled(true)
                    .setSendPresence(false)
                    .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) 
                    .build();
    
    
            AbstractXMPPConnection connection = new XMPPTCPConnection(config);
    
            try {
                connection.connect().login(userName, userPassword);
                return connection;
            } catch (XMPPException | SmackException | IOException | InterruptedException e) {
                System.out.println("Something went wrong while connection to server: " + e.getMessage());
                connection.disconnect();
                return null;
            }
        }

and offline messages receiver

private XMPPConnection connection;
    
        @Override
        public void run() {
            connection = Connector.getInstance();
    
            if (connection != null) {
                System.out.println("Start listening messages in the background");
    
                String offlineMessageOffline = "Offline message is %s from %s";
    
                OfflineMessageManager offlineMessageManager = new OfflineMessageManager(connection);
                try {
                    if (offlineMessageManager.supportsFlexibleRetrieval()) {
                        offlineMessageManager.getMessages()
                                .forEach(message -> {
                                    System.out.println(String.format(offlineMessageOffline, message.getBody(), message.getFrom()));
                                });
    //                    offlineMessageManager.deleteMessages();
                    }
                } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException |
                        SmackException.NotConnectedException | InterruptedException e) {
                    System.out.println(String.format("Error while getting offline messages from server: by the reason %s", e.getMessage()));
                }
                try {
                    connection.sendStanza(new Presence(Presence.Type.available));
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                ChatManager chatManager = ChatManager.getInstanceFor(connection);
    
                IncomingChatMessageListener listener = (entityBareJid, message, chat) -> {
                    System.out.println(String.format("Incoming message is %s from %s", message.getBody(), message.getFrom()));
                };
                chatManager.addIncomingListener(listener);
            } else {
                System.out.println("Connection has not been established. Please try to choose option 1");
            }
        }

I start running message listener at the background. I see that database has an offline message and in the dubug node I see that offlineMessageManager.getMessageCount() returns the proper number of offline messages but method offlineMessageManager.getMessages() returns noting. The smack version is 4.2.3 but earlier version has the same problem


#2

Please have a look at https://github.com/igniterealtime/Smack/wiki/How-to-ask-for-help,-report-an-issue-and-possible-solve-the-problem-yourself

Especially an XMPP trace would be helpful.