Search code examples
serviceandroid-servicekotlin-coroutines

How to call suspend function from FirebaseMessagingService?


I need to call suspend function from onMessageRecieved(), but this service doesn't provide a scope. So I don't know from where to call job.cancel(). How to use suspend function in this case?


Solution

  • FirebaseMessagingService is a derived class of Service, so you can use it's onDestroy() to cancel your coroutines.

    override fun onDestroy() {
        super.onDestroy()
        job.cancel()
    }