what should I use when I need to work with backgro...
# android
a
what should I use when I need to work with background in kotlin?
m
I believe you are asking about
BroadcastReceiver
a
nope ,whenever I get the trigger when the app is in background then I wont be able to open app in foreground right ?
broadcast receiver only sends the triggers or events , but it wont open the app to foregrounf
m
You have to start the application in onReceive ofc
m
if you mean background work when app is not open, then use
WorkManager
. If you mean background app when app is open, but you are doing something in the background (for example refreshing some data for the UI to display), then use
coroutines
.
a
I mean , I need to open the app after getting notification or trigger from other server when the app is in background
so for this I need to prefer work manager or coroutines?
anyone knows?
m
you use Firebase cloud messaging for that
also this has nothing to do with Kotlin
✔️ 3
a
sir I am asking not only about notification , any triggers which I get in background like MQTT server
m
AFAIK you cannot listen to MQTT in the background for more than few minutes since Oreo unless you request battery exclusion (which Google has to explicitly approve to reach Play Store, and good luck with that)
a
so how does the calling in whatsapp listen to new calls and open the app ?
m
firebase
y
The notification calls an intent to the app, who registers in its android manifest
Whenever you need to click something and open your app, just register in the android manifest a intent filter who knows where to open
p
Those days where we could run Services forever or let our App alive forever are gone. There is only a few alternatives for doing so. 1- Firebase Messaging to send High Priority Messages Notifications that wakes up your App. On Battery Saver it can get dealyed. 2- Foreground Service. Your App runs Forever. Make sure to get Wifi.Awake locks so the radio transceiver don't fall to sleep 3- WorkManager. It has its restrictions. Doze, Standby and Battery Saver applies to it. It smell like the next step is deprecating our beloved Services. The old Trick with continues Alarms awaking the App will be gone soon, I read it the other day in one channel.
a
thanks a lot guys