java - React Native: sending events from android to javascript - Stack Overflow

there is documentation available on how to municate from RN javascript parts with native parts in andro

there is documentation available on how to municate from RN javascript parts with native parts in android and ios over the RN bridge. What is kind of unclear is the lifecycle for bridge munication.

(source medium)

I have a background service in android and 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 a ponent, just code imported to 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 runs even when the app is in the background. But I want to make sure that I only try to municate with the javascript side when it is safe to do so.

I sometimes get errors such as React: Calling JS function after bridge has been destroyed. Is there a way to find out on the native (android) side wether the bridge is available or not?

This is not about how to keep background code alive but rather about to make sure that as long as my native (android) code is running, I can ensure that events are not lost.

there is documentation available on how to municate from RN javascript parts with native parts in android and ios over the RN bridge. What is kind of unclear is the lifecycle for bridge munication.

(source medium)

I have a background service in android and 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 a ponent, just code imported to 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 runs even when the app is in the background. But I want to make sure that I only try to municate with the javascript side when it is safe to do so.

I sometimes get errors such as React: Calling JS function after bridge has been destroyed. Is there a way to find out on the native (android) side wether the bridge is available or not?

This is not about how to keep background code alive but rather about to make sure that as long as my native (android) code is running, I can ensure that events are not lost.

Share Improve this question edited Mar 29, 2018 at 12:45 oliver asked Mar 21, 2018 at 15:52 oliveroliver 9,4954 gold badges36 silver badges39 bronze badges 5
  • 1 You need to add some detail and code. This is a very broad question as the rules around background services are very plex for both iOS and Android. Especially so if you are targeting Android O and above. You are going to have to narrow this down a lot. – Michael Cheng Commented Mar 21, 2018 at 16:29
  • @MichaelCheng Is it really that broad? I use events as a mechanism to municate and try to find a way make sure events are not missed. I added the relevant parts of my code though. – oliver Commented Mar 21, 2018 at 18:56
  • The broad part is how your background service is coded. iOS and Android have very strict rules about how long a background service can be kept alive, what permissions must be granted to keep them alive, and even then, they can be killed if deemed necessary by the scheduler. To plicate this, these rules and how this is supposed to be done changes depending on what range of OS versions you plan to support. Then to top that off, there are ways to restart your app once killed. How you work around all of these use cases is why I asked to see code. (...continued in next ment) – Michael Cheng Commented Mar 21, 2018 at 19:16
  • 1 Now that you've shown some code, I can see that you're just doing a basic callback and am assuming that your service isn't doing anything fancy to keep itself alive or restarting the app. So there's a good chance that your background service is going to get killed as well. Without knowing what use cases you are trying to cover for, it's hard to give a good remendation. For example, you can try to code it to keep your app alive, or you can assume it will die and store those events in a queue that can be saved to disk that is processed instead of fired immediately, etc, etc. – Michael Cheng Commented Mar 21, 2018 at 19:22
  • thanks @MichaelCheng. my problem is not so much about keeping my service alive. It's rather how to make sure that events that I send from android actually can be sent over the bridge. I just want to make sure that as long as my service runs, I don't loose events and only use the bridge if it is available. – oliver Commented Mar 21, 2018 at 19:39
Add a ment  | 

1 Answer 1

Reset to default 3

The issue occurs when the corresponding ReactContext/CatalystInstance has already been destroyed, and you are trying to do some JS stuff from native side (like sending events, doing callbacks etc).

I guess your best bet is to use lifecycle callbacks, i.e. onHostResume, onHostDestroy (https://facebook.github.io/react-native/docs/native-modules-android.html#listening-to-lifecycle-events) or use another callback onCatalystInstanceDestroy(https://github./facebook/react-native/blob/26684cf3adf4094eb6c405d345a75bf8c7c0bf88/ReactAndroid/src/main/java//facebook/react/bridge/NativeModule.java#L51)

Send events only when you know it is not destroyed.

The core idea is that usually the react context is bound to a given Activity/Context in your app, and you can know that lifecycle via callbacks in NativeModule.

You could find more details probably at (https://github./facebook/react-native/blob/b531612b2c917e1f2bd6bb37bf854fe51e658b36/ReactAndroid/src/main/java//facebook/react/ReactInstanceManager.java#L104)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745629651a4637027.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信