I've been using Smack XMPP android library for chat application.
My current architecture for chat is ,I use XMPP to send messages to client only if the app is in Foreground otherwise use GCM. Server is already handled properly.
Since android doesn't have method to simply indicate where the app is currently in FG or BG, I find difficult connecting and disconnecting XMPP . So I implemented using timer on each activity onPause and onResume when the app has more than one activities.This is very painful and lot more work to do.
For eg , I have two activities, A and B.Connect XMPP in onResume() of A .Switch from A to B, onPause() will trigger on A and disconnect XMPP connection after 3sec.Within 3sec , onResume() of B will trigger and stop the timer so that connection will be alive . :-)
onPause()
if (Application.getInstance().xmppStatus == XMPPSTATUS.CONNECTED) {
Application.getInstance().disconnectXmpp(); //disconnect xmpp after 3sec
}
onResume()
if (Application.getInstance().xmppStatus == XMPPSTATUS.DISCONNECTED) {
LoginHelper.connectXmpp();//connect XMPP
}
I believe that most of you have fallen into this kind of issue esp in chat so i would very appreciate to hear your experiences on this.