2

I'm looking for some way to execute JS code from Java Native Module. I found solution with Timer, but it use Callbacks and it works only when app is in background (React-native background process(android) handling?).

I'm using AlarmManager to wake up Java's BroadcastReceiver, but I have no idea how, from this point, run Javascript code. This code isn't a React Component, but it use some react sdk and modules (facebooksdk, googlesdk, Keychain, Asyncstorage).

I tried to create listener on DeviceEventEmitter (due to https://facebook.github.io/react-native/docs/native-modules-android.html) and run in BroadcastReceiver something like

public class Alarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    ReactApplicationContext contxt = (ReactApplicationContext) context;
    contxt.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
    .emit("runTask", null);

}

But I have cast runtime error:

Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to com.facebook.react.bridge.ReactApplicationContext

Maybe it's not possible to run JS from Java Module in background, especially when application is closed. Any tips are welcome.

Community
  • 1
  • 1
jonzee
  • 1,600
  • 12
  • 20

1 Answers1

3

Check out the transistorsoft github account

Specifically:

I've had some success with these.

However they all have their drawbacks. Up to this point I have not found a package that is ACTUALLY running JS in the background. The RN team is supposed to be working on it, but nothing yet.

The solution in this case would be to code whatever you need to run in the background in java. Ultimately this is what I did.