Periodically check if socket connected using workmanagerfrom 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
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:
- If your service is already running, you get the extra
- If your service is not already running, it starts the service
Mostly, that will deal with crashes and any devices that ignore
However, it will not help with these concerns:
- Doze mode will interfere with any periodic work, so the heartbeat will not be beating all the time
- Force stop will cancel that periodic work
I do not know of any good solution for those, sorry.