I have background service in Android to handle Google Firebase Push Notifications:
class MyFirebaseMessagingService : FirebaseMessagingService() {
@Inject
lateinit var repository: Repository
@Inject
lateinit var coroutineDispatchers: CoroutineDispatchers
private val serviceJob = Job()
private lateinit var serviceScope: CoroutineScope
override fun onCreate() {
super.onCreate()
AndroidInjection.inject(this)
serviceScope = CoroutineScope(coroutineDispatchers.default + serviceJob)
}
override fun onNewToken(token: String) {
// Not important
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
serviceScope.launch {
try{
Timber.e(repository.someSuspendMethod())
} catch (e: Exception){
Timber.e(e)
}
}
}
override fun onDestroy() {
super.onDestroy()
serviceJob.cancel()
}
}
My problem is that job is getting cancelled before finishing, because onDestroy() is called. Any idea why Service is killing itself before job is done?