powered by Jive Software

Help with OpenFire and Smack (Offline message retrieval)

Hi everyone,

I’m having a hard time figuring out how to retrieve offline messags with the Smack API in conjunction with OpenFire. I have sample code that logs in with one user and sends a message to an offline user. I then have the recipient login and try to retrieve the offline messages. At this point, according to the logs, The offline message is sent back immediately after the login and presence is sent. There is no way for me to “catch” this offline message. I tried using the OfflineMessageManager included in the Smack API but by the time I invoke it the offline message has already been sent. Here is my code. Anyone have a working example of how to use OfflineMessageManager?

Thanks,

Ish

public class TestOfflineMessages {

private XMPPConnetion _conn1;

public void loginAndRetrieveOfflineMessages()

{

_conn1 = new XMPPConnection(“localhost”);

try {

_conn1.connect();

        _conn1.login("testreceiver@mydomain.com", "pw");  

OfflineMessageManager offlineMgr = new OfflineMessageManager(_conn1);

if(offlineMgr.supportsFlexibleRetrieval())

{

Iterator<Message> it;

it = _offlinemgr.getMessages();

while(it.hasNext())

{

System.out.println("— " + ((Message)it.next()).getBody());

}

}

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Have you tried instanciating the OfflineMessageManager before logging in, after connecting?

Yes I have tried instantiating the OfflineMessageManager with the connection object prior to login. However the offline messages still get sent back immediately after login, and the OfflineMessageManager does not have a handle to them. Is there a way to “catch” these offline messages? How do most clients that use the Smack API show offline messages?

Ish

I figured it out finally after downloading the Smack source and looking at the Junit test cases in detail. The problem was during logging in I was automatically sending my presence. I suppose when a jabber server gets presence it automatically sends all offline messages. Therefore a simple change in my login to NOT set my presence allowed me to fetch offline messages at my leisure. Updated source code is below for others that encounter the same problem.

public class TestOfflineMessages {

private XMPPConnetion _conn1;

public void loginAndRetrieveOfflineMessages()

{

_conn1 = new XMPPConnection(“localhost”);

try {

_conn1.connect();

OfflineMessageManager offlineMgr = new OfflineMessageManager(_conn1);

// Make sure to set send presence to false

_conn1.login("testreceiver@mydomain.com", “pw”, “Smack”, false);

if(offlineMgr.supportsFlexibleRetrieval())

{

Iterator<Message> it;

it = _offlinemgr.getMessages();

while(it.hasNext())

{

System.out.println("— " + ((Message)it.next()).getBody());

}

}

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

i tried this and i get this error message:

07-01 22:31:22.893: E/AndroidRuntime(7075): Caused by: java.lang.ClassCastException: org.jivesoftware.smack.util.PacketParserUtils$UnparsedResultIQ cannot be cast to org.jivesoftware.smackx.packet.DiscoverInfo

Does nobody else get it? or does anyone know what i need to do to fix it?