Retrieving offline messages using asmack

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> offlineMsgs = new HashMap<String,ArrayList>();

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 temp = new ArrayList();

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.

You have to establish a presence session, by sending an available presence. Something like

connection.sendPacket(new Presence(Type.available));

I saw, you manually switched it off (connConfig.setSendPresence(false):wink:

EDIT: Oh, I saw, you doing it already.

Also make sure, to add the PacketListener to the connection BEFORE you login.

I’ve tried it that way too. Doesn’t work. I’d read that you’ve to keep presence off first, then use the OfflineMessageManager and then retrieve messages before setting presence to available. Anyway, none of these ways worked for me.

Edit:I’ve set the packet listener before logging in and even that doesn’t work

Only use OfflineMessageManager, if you don’t want to get the offline messages automatically (XEP-0013).

Otherwise just add your PacketListener to the connection, then login, then send presence (if you switched it off). It works for me.

You can also try setting Connection.DEBUG = true to see the XMPP traffic. Do you see any messages there?

Theres no Connection.DEBUG method, however in the connection configuration you can set Debugger as enabled, which I did. I get the XMPP traffic in my logcat. So I sent a message from one account to the other. When I send I can see this

04-04 19:37:05.828: D/SMACK(14369): 07:37:05 pm SENT (1115328480): koi

Now when I login to the other account, I don’t see any message received in the logcat. If you’ve this working, could you post a sample code of what you have?

My bad, I was logging in my login activity first and then sending the username and password to the main chat activity and logging in again. The messages were being sent to the first activity which didn’t have a packet listener set before login. I set the packet listener before login there and I can see all the offline messages just fine. Thanks for your time tho