Hey there. Here's a question regarding an Android ...
# android
s
Hey there. Here's a question regarding an Android Service + Coroutines. We implement FCM for push messaging. When a push arrives, we might have to fetch an image before display it, and that fetch runs in a suspend function. But since the onMessageReceived is a non-suspend function I need to enter into a suspend context. Right now we're doing that with runBlocking so that onMessageReceived only returns once we're done fetching the image.
Copy code
class FirebaseMessagingServiceImpl : FirebaseMessagingService() {
    override fun onMessageReceived(message: RemoteMessage) {
        runBlocking {
            notificationService.show(message)
        }
    }
}
Is that they way to go? We've tried with a
GlobalScope.launch {}
as well- it seems to work just as well, but I'm worried that Android might potentially kill the process before the job has finished because onMessageReceived returns immediately.
r
I'd say 1. use a local image if possible 2. if fetching a remote image, I do not recall any specific hard restrictions on how long this service is allowed to be alive, because onMessageReceived is only triggered if the app is in foreground, and I doubt you need to worry about the user closing the app immediately and the system then killing the service and the process before it can fetch the image and display the notification. Also, not kotlin but kotlin colored
s
Hey- thanks for the response. I can't use a local image unfortunately. The images are content-previews. onMessageReceived is also triggered when in background for data messages and has a stated limit of 20 seconds. That's more than enough time to fetch a thumbnail (or time-out if we're getting close to that limit). So I don't think the execution limit is all too important for us. Regarding not-kotlin: this question is fairly strongly connected to coroutines, which is Kotlin. I could move this to the coroutines channel if this were better suited, but it's also connected to Android 🤷
r
Yeah, I'd say if 10-20 seconds isn't enough then the official Android guideline is to submit a workmanager request
m
I just read up on this thread and I also agree that you should submit work manager request.
👍 1
1
s
Work Manager is what I ended up doing (OneTime requests for onMessageReceived and onToken each). Seems to work like a charm K