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.