powered by Jive Software

Keeping connection alive even on application close

Smack version - 4.3.1

I am noob to android and SMACK . Don’t mind the silly doubts.

We are working on a chat app using XMPP and smack as XMPP client. We want the following functionality.

  1. App should be in keep alive state.
  2. App should remain connected to the server even if user closes the app in backgroud.
  3. The only condition when it is disconnected is the user phone internet is not working or the server.
  4. If the user logged in one time (while installing the app) then what should be the flow and where to place the code so the reconnection process happens at any activity through out the application .

Use a sticky background service to keep the connection running.

Thanks @Paul_Schaub,
Just want to confirm that a normal ping manager code and disconnect handlers will work in the “sticky background service” you mention??
Or somthing else should be coded for this.

The handlers should work as usual. For ping, I’d recommend taking a look at the ServerPingWithAlarmManager from smack-android-extensions.

You can use foreground service as well if you are okay to show a notification all the time.

Can you please let me know your why you wanted to connected all the time it will cause battery drain issues may be i can help you get out of it.

Staying connected all the time actually does not cost much battery at all (given the right protocol extensions like CSI, Stream Resumption are enabled).
My test application does not even show up in Androids battery stats.
And yes, you probably need a foreground service on some devices.

I found the approach of WorkManager (https://developer.android.com/topic/libraries/architecture/workmanager) + Firebase Cloud Messaging (https://firebase.google.com/docs/cloud-messaging) gives the best, consistent performance. Using a foreground service may work if you don’t mind your users seeing a visible notification, and using a background service may result in all sorts of unexpectedly killed tasks, especially on phones coming from China (see https://dontkillmyapp.com).

That being said, even WorkManager + FCM may not survive the nasty approach used in Oppo, for one.