I'm working on a VoIP app that receives high priority notifications from Twilio when an incoming call happens.
Sometimes, when this happens I'm getting the following exception, which is a horrible time to crash as it forces the call to voicemail on the other end.
Fatal Exception: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.incomingCall cmp=com./.service.VoipService (has extras) }: app is in background uid UidRecord{368a9a8 u0a186 CEM idle procs:1 seq(0,0,0)}
The exceptions are all happening on Samsung Galaxy S7's running Android 8.0, which a lot of our users use.
I get the remote message in onMessageReceived
, parse some data out of it, and then call Voice.handleMessage(this, data, twilioMessageListener)
which does a tiny amount of Twilio logic, and then triggers the callback
private var twilioMessageListener: MessageListener = object : MessageListener {
override fun onCallInvite(callInvite: CallInvite) {
val intent = Intent(this@FcmListenerService, VoipService::class.java)
with(intent) {
action = Constants.ACTION.INCOMING_CALL
putExtra(Constants.EXTRA.INCOMING_CALL_MESSAGE, callInvite)
putExtra(Constants.EXTRA.INCOMING_CALL_MESSAGE_STATE, callInvite.state)
}
startService(intent)
}
}
And then crashes on start service.
According to the documentation
Under certain circumstances, a background app is placed on a temporary whitelist for several minutes. While an app is on the whitelist, it can launch services without limitation, and its background services are permitted to run. An app is placed on the whitelist when it handles a task that's visible to the user, such as:
Handling a high-priority Firebase Cloud Messaging (FCM) message.
So I'm not sure what I'm doing wrong, or what I can do to resolve this?
It's a high priority FCM and I'm still getting the error?
Is this a bug in the Samsung S7's OS? Is there something I can do to work around it? I've tried putting my devices into doze, but I can't replicate it.