What should we use for long running tasks that sho...
# android
k
What should we use for long running tasks that should survive process deaths or app restarts?
CoroutineWorker
with
foregroundInfo
isn't working in my case. I've opened up a websocket connection and want to listen for messages in background forever.
g
You cannot survive process death, on newer androids you also cannot do anything forever in background, so Foreground service (which prevents process death) is the only real way
👍 1
k
Okay.. so the best I could get is running it when is in background.. beyond that it won't be possible? Then how does chat apps like whatsapp receive notifications?
a
Probably they are using push notifications
Witch are handled by system, and wake up application
e
Firebase push notification would help you with that probably.
k
Okay.. I had to stick to websockets.. so may be setForeground is the best I could get
g
Whatsapp and all other chat applications use FCM messages for background
k
Okay, thanks!
t
I'm curious about your use case. Keeping an active websocket active forever does not sound like a great idea, unless you have incredibly scalable servers. If you are sending/receiving data at slow pace (less than one message per minute), maybe you'd rather need request polling. Websocket are more for receiving/sending data at a fast pace, for example realtime applications
n
as @tseisel says it's not good idea but also is may be possible by regester broadcast receiver fires when the OS connect to the internet will notify your recevier and doing yor logic , but still not good solution you need to integrate FCM admin with your web app.
k
@tseisel we've only one way communication, I've to receive messages from server only. So ideally I should be using Server Side Events for this but the backend is in c++ and they found implementing websockets easier than SSE.