Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send XMPP message when sender is offline?

I am using aSmack and Openfire for my chat application. I am able send and receive message finely. Openfire support offline message transfer when recipient is offline by keeping message until he comes online.

But what to do when sender is offline or his internet drops between communication?

Is there any api provided by aSmack/Smack which keeps message until internet is back ?

Or should i send my messages through SQLite ?

like image 972
Sushant Avatar asked Mar 29 '15 05:03

Sushant


2 Answers

If you are connected to openfire and internet goes off then you are still online on openfire because you can't change presence if internet is off.

For this openfire use http://xmpp.org/extensions/xep-0199.html

If user app doesn't reply ping request then openfire makes it offline and offline storage starts.

For getting offline messages in asmack you need to add following providers.After adding these you will get offline messages if they are enabled from the server

pm.addIQProvider("offline", "http://jabber.org/protocol/offline",
                new OfflineMessageRequest.Provider());
        // Offline Message Indicator
        pm.addExtensionProvider("offline",
                "http://jabber.org/protocol/offline",
                new OfflineMessageInfo.Provider());

enter image description hereions/xep-0199.html

like image 192
Jaspreet Chhabra Avatar answered Oct 22 '22 09:10

Jaspreet Chhabra


Well answering after a while, but perhaps still it would point someone in right direction.

After searching for a while, i get to know that aSmack doesn't provide any offline storage.

To send message when sender is offline, we need offline storage to store message until sender gets back online. I used sqlite for this, but SharedPreferences would also be a good option if we have to delete message after successfull sending.

Best solution, which i follow is, insert message in sqlite, store it's id in shared preferences, send message and then remove id from shared preferences. In mean while if intenet connection drops or message couldn't send by any reason, then we have it's id in shared preferences as backup. After getting online, we can go through shared preferences to check if their is any message is pending to send.

like image 33
Sushant Avatar answered Oct 22 '22 10:10

Sushant