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
13 Comments
David
about 9 years agoHi! 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.
Replyedwin
about 9 years agohi 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 9 years agoYes 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.
ReplyDavid
about 9 years agoWhen 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 9 years agoCould 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.
Replyedwin
about 9 years agoHi 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 9 years agoConnectionConfiguration 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.
Replyedwin
about 9 years agoare 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 9 years agoYes, 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 7 years agohi 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
Replyedwin
about 7 years agoHi 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.
Ahmad
about 6 years agoThank, very useful
ReplyHector
about 6 years agoJust to remind. Don't forget to add "ProviderManager.getInstance()addExtensionProvider("x","jabber:x:delay", new DelayInformationProvider());" to your connection
Reply