How to handle SIP logic in Android headless task

I’m developing a VoIP app using Callkeep and Sip.js. I’m setting up my SIP handling and Callkeep logic in a useEffect at the root of my app. So I’m creating and starting my user agent, registering, and setting up Callkeep event listeners. In my onInvite delegate of my user agent, I’m passing a function that listens for change events in the SIP session state and responds by setting UI state and navigating to appropriate screens. On iOS, this approach works fine because when my app is in a killed state and I receive an incoming call, the VoIP notification wakes up my app in the background, starting the useEffect and triggering my app to connect to my SIP server and properly handling state updates so that once my app returns the foreground, it will be in the correct state. However, on Android this approach does not work. When my app is the foreground, everything works as expected. But when my app is in the killed state, I have to respond to the incoming call notification through a headless task. Calling RNCallkeep.displayIncomingCall from the headless task works but does not start my app in the background so the useEffect with my SIP handling logic does not get called so I don’t have a chance to register to my SIP server and the call fails. It seems like I should move the SIP handling logic to the headless task but then I wouldn’t be able to properly update my UI state and navigations in response to the SIP events, so when my app returns to the foreground it would not be in the correct state. Any tips on how to go about solving this issue would be appreciated.

There are many different approaches to solve the issue.
Android is a bit of a mixed bunch.
One of the common ways would be to save states in the headless task.
Then check and update them states once the app gets brought to the foreground.