How to Get Offline Message’s Timestamp on Openfire

I had a simple project using Openfire and Smack library, i found an obstacle when i had to display offline message’s timestamp. I usually use local timestamp for simple messages, but when dealing with offline messages i had to use server’s timestamp, that’s the tricky part.

It is actually very simple to solve it, only several lines of codes. But i’ve spend ridiculously amount of time searching the net for it. That’s why i try to posted it in my blog in case of others might need it.

This is an ordinary packet received,

<message id="Vw7cj-13" to="gajah@edw/Smack" from="kelinci@edw/Smack">
  <body>ordinary message</body>
</message>

while this is what received offline packet looks like

<message id="Vw7cj-12" to="gajah@edw/Smack" from="kelinci@edw/Smack">
  <body>example of offline message</body>
  <thread>U249D0</thread>
  <x xmlns="jabber:x:delay" stamp="20110625T06:53:52" from="edw"/>
</message>

As you can see, offline messages send its timestamp on line 4. Now the problem is how to get it.
But before testing, dont forget to store your message offline policy. And btw, my server name is “edw”.

This is how i solve it,

DelayInformation inf = null;
try {
	inf = (DelayInformation)packet.getExtension("x","jabber:x:delay");
} catch (Exception e) {
	log.error(e);
}
// get offline message timestamp
if(inf!=null){
	Date date = inf.getStamp();

And please dont forget to include smackx.jar.

Im using Openfire 3.6.4 and Smack. I am very recommend these libraries for creating a simple yet powerful instant messaging application based on Java. Have fun :)

Google+

11 Comments

David

about 3 years ago

Hi! I use openFire, I store all offline messages. However, the timestamp on offline messages is empty. Is all I get, know anything about it? Have searched a lot but didnt find anything.

Reply

edwin

about 3 years ago

hi David, try to check your ofoffline table, does it contain creationDate field? and set your openfire log into debug and see if there is any exception happen.

David

about 3 years ago

Yes it contains a creationDate field. Weird thing is that using IE Adium (jabber client for mac) it shows me time stamp on offline messages. It's a multi user chat Im using. And the multiuserchat package listener shows me a delay field when I log the package in xml but its just empty.

Reply

David

about 3 years ago

When I edit the offline table it seems to be empty. So Im not sure if Im looking at the wrong database. Its named ofOffline and the creationDate field is char(15),not null

David

about 3 years ago

Could the problem be that its a multi user chat? An old post, but the same problem: http://community.igniterealtime.org/message/187288#187288 But adium somehow gets the timestamp.

Reply

edwin

about 3 years ago

Hi David, sorry for the late response. Could you please paste your source code, especially in the message sending part. If im not wrong, MultiUserChat is for group chat and stuffs. I send message to other users using these codes, Message msg = new Message(to, Type.normal); msg.setBody(textChat); msg.setFrom(connection.getUser()); connection.sendPacket(msg);

David

about 3 years ago

ConnectionConfiguration connConfig = new ConnectionConfiguration(domain); connConfig.setSelfSignedCertificateEnabled(true); connConfig.setSecurityMode(SecurityMode.disabled); XMPPConnection xmpp = new XMPPConnection(connConfig); xmpp.login(userName,passWord); MultiUserChat muc = new MultiUserChat(xmpp, channel); muc.sendMessage(textToSend); Thats more or less it, I am using asmack (smack for android). Doesnt seem to matter if I send the messages through a jabber client or from my phone. The interesting part is that other jabber clients somehow gets the timestamp from the offline messages but I dont get them using smack.

Reply

edwin

about 3 years ago

are you trying to send messages to a conference? What's your gtalk or skype id, perhaps it will be better if we chat instead of replying here.

David

about 3 years ago

Yes, there is no problem in sending. It recieves without problems. But fetching offline messages (the first messages I get) when adding a packageListener to muc doesnt have timestamp in their delay attribute.

vaibhav

about 3 weeks ago

hi guys, is possible push notification service in openfire server.i want to create an plugin for push notification when users is offline and show notification message . i search more and more but did n't find any solutions. please give me any solutions

Reply

edwin

about 3 weeks ago

Hi Vaibhav, the offline message are stored somewhere in the database, if im not wrong on ofOffline table. You could create a cron job to query on that table and fetch the content.

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked


:-[ (B) (^) (P) (@) (O) (D) :-S ;-( (C) (&) :-$ (E) (~) (K) (I) (L) (8) :-O (T) (G) (F) :-( (H) :-) (*) :-D (N) (Y) :-P (U) (W) ;-)