7

I need to keep a service running in background. This service is used to keep a Socket.IO/MQTT connection. In versions previous to Android O there were no problem keeping the service alive, but in Android O this is not possible because of the background execution limits.

Is there any way to keep the services running, besides foreground service?

Mariano Córdoba
  • 1,097
  • 1
  • 13
  • 29
  • 5
    Nope. You'd need a persistent notification as a foreground service to keep it consistently running. – TomH Jul 06 '18 at 22:21
  • Background execution limit was made especially to counter heavy battery drain of multiple sockets. Otherwise resort to periodic jobs. – Pawel Jul 06 '18 at 22:23
  • Your service can run in the background for *5 seconds* before you need to attach a notification to it. You might wanna read all about [Android O background limitatons](https://developer.android.com/about/versions/oreo/background) – Sourabh Jul 07 '18 at 17:34
  • are you solve this issue? @Mariano Cordoba – Revathi Manoharan Aug 02 '18 at 06:47
  • @RevathiManoharan no. I'm using a foreground service for persistent connections and workmanager for tasks that are executed at fixed rates. – Mariano Córdoba Aug 02 '18 at 14:42
  • Ok let try jobIntent too.but my redmi note pro device working with when app fully closed i tried to push notify then my app got wokeup..It's achieved without using job intent here i used only startForegroundService.but same scenario not working in One plus device.i thought its devices specific problem.so u have to try someother android devices too – Revathi Manoharan Aug 02 '18 at 14:47
  • In redmi note 5 pro while app is in background when I tried push notification its working – Revathi Manoharan Aug 02 '18 at 14:48

1 Answers1

0

You should use Firebase Cloud Messaging to push data from a server, this is to prevent all apps opening their own connection and staying active, draining the battery and filling up the RAM for no value to the user. Your app should be no exception to that.

For short lived connections (minutes), or user sessions (minutes, too, unless the user stays active longer), a foreground service might be a solution.

Out of curiosity, what is your use case for wanting a "persistent connection"?

Louis CAD
  • 10,965
  • 2
  • 39
  • 58
  • I need to keep track of the presence of the user. I don't think FCM would help in this case, probably the best choice is to use a foreground service. – Mariano Córdoba Mar 30 '19 at 17:17
  • What do you consider "presence of the user"? – Louis CAD Mar 30 '19 at 18:43
  • Also, do you really need it to be sent over the internet in real-time (I mean seconds, which still depends on network availability)? – Louis CAD Mar 30 '19 at 18:44
  • By presence i mean if the user is online or not. And yes, i need it to be near-real-time. – Mariano Córdoba Mar 31 '19 at 15:37
  • That doesn't answer my first question. I can rephrase it this way: When do you consider the user is no longer online, and when do you consider he is back online? – Louis CAD Mar 31 '19 at 16:06