Periodically check if socket connected using workmanager

from the CommonsWare Community archives

At June 5, 2019, 6:07am, rd7773 asked:

Basically want to build a system that doesn’t miss any incoming events in realtime.
Have been using socket.io and sticky service, but seems like few chinese oems simply do not allow sticky service (auto start) permission. Then there is Doze mode putting limitation to any background work running along with other oem specific limitations like force stop of app on clearing from task manager.

What are your suggestions for building an app that receives real time notifications even when app is killed or in background.


At June 5, 2019, 10:40am, mmurphy replied:

The Google-preferred answer is FCM, but that may not help you with your problematic devices, as they may not have Play Services.

I assume that your sticky service is also a foreground service. If so, then you might use WorkManager, JobScheduler, or AlarmManager simply as a “heartbeat”, starting that service if it is supposed to be running. So long as your service can deal with receiving onStartCommand() more than once, this sort of heartbeat check should cause no harm:

Mostly, that will deal with crashes and any devices that ignore START_STICKY.

However, it will not help with these concerns:

I do not know of any good solution for those, sorry.