there is documentation on how to communicate with JavaScript RN parts with native parts in android and ios over the RN bridge. What is unclear is the life cycle for communication with the bridge .

(source medium )
I have a background service in android and I need to send events to my RN application. This is how I send events from my Android Service :
private RCTDeviceEventEmitter emitter() { return mReactContext.getJSModule(RCTDeviceEventEmitter.class); } private void pushPayload(String event, WritableMap payload) { emitter().emit(event, payload); } public void sendTimerEvent(long remaining) { WritableMap data = Arguments.createMap(); data.putInt("remaining", (int) remaining); pushPayload("timeoutEvent", data); }
On the javascript side, I use the following static code (not in the component, just the code imported into my index.android.js file:
const subscribeForNativeEvents = (eventID, callback) => { const Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter; Emitter.addListener(eventID, callback); }; subscribeForNativeEvents('timeoutEvent', (event) => { // work with event.remaining });
My service starts even when the application is in the background. But I want to make sure that I'm only trying to contact the javascript side when it is safe.
Sometimes I get errors like React: Calling JS function after bridge has been destroyed . Is there a way to find out whether the modem is available on the native (android) side or not?
It's not about how to save the background code, but rather, to make sure that as long as my own (android) code is running, I can guarantee that the events will not be lost.
source share