8

I want to call a method in MyCustomModuleClass extends ReactContextBaseJavaModule from another Class

MyCustomModule.java

public class MyCustomModule extends ReactContextBaseJavaModule {

  private ReactContext mReactContext;

  public MyCustomModule(ReactApplicationContext reactContext) {
    super(reactContext);
    mReactContext = reactContext;
  }

  @Override
  public String getName() {
    return "CustomModule";
  }


    private void sendEvent(String eventName, Object params) {
        mReactContext
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(eventName, params);
    }    
}

I'm trying this..

OtherClass.java

public class OtherClass extends AnotherClass {

    @Override
    protected void eventOccurred(Context context) {
        MyCustomModule RNC = new MyCustomModule(?);//-> Can't call without ReactContext
        RNC.sendEvent("CustomEvent", ObjectData); 
    }   
}

Sorry, I know nothing about java, could any one please help me out?

Is there any way I can get ReactContext in OtherClass?

5
  • It does not look like your calling a valid constructor on MyCustomModule. Maybe thats just a typo you made when trying to create a simple example. I don't see why you could not do that. Just try it. Commented Feb 18, 2016 at 16:04
  • Yes, I was doing it all wrong. Let me update my question. Commented Feb 19, 2016 at 12:35
  • I solved my problem by another way, using LocalBroadcastManager. Commented Feb 19, 2016 at 12:43
  • @antihate - Would you mind sharing your solution? This problem is driving me nuts.. Commented Nov 1, 2016 at 17:41
  • this question got more feed back in the issues: github.com/facebook/react-native/issues/5846 Commented Apr 29, 2018 at 6:54

1 Answer 1

8

Here's how I did it in a FirebaseMessagingService extended class.

public class MyPushListener extends FirebaseMessagingService {

  @Override
  public void onMessageReceived(JSONObject message, JSONObject content){

    MainApplication application = (MainApplication) this.getApplication();

    ReactNativeHost reactNativeHost = application.getReactNativeHost();
    ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
    ReactContext reactContext = reactInstanceManager.getCurrentReactContext();

    if (reactContext != null) {
      WritableNativeArray params = new WritableNativeArray();
      params.pushString(message.toString());
      reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
          .emit("EVENT_HAS_TRIGGERED", params);
    }
  }
}

Source

Sign up to request clarification or add additional context in comments.

1 Comment

I also did this but with RN 0.71.8 I see this warning for getCurrentReactContext();` This method should only be accessed from tests or within private scope`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.