votrai123 / integrate-multiple-react-native

8 stars 4 forks source link

Send data from mini app to Super App #11

Open binhit92 opened 7 months ago

binhit92 commented 7 months ago

How i can send data from mini app to super app? i was call sendMessage in native code but only recived in mini app.

thinh-nn3386 commented 1 month ago

Sending Data Between Mini App and Super App You can send data from a mini app to a super app or vice versa using Send Events via Native Module.

  1. iOS: It's easy to follow the instructions provided in the documentation.

  2. Android: It's a bit more tricky.

By following these steps, you can successfully send events between a mini app and a super app on both iOS and Android platforms.

private BroadcastReceiver myReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (BROASCAST_EVENT.equals(action)) {
      Bundle bundle  = intent.getBundleExtra("data");
      String type = intent.getStringExtra("type");
      WritableMap params = Arguments.createMap();
      if (type != null && type.equals(DEEPLINK_EVENT_NAME)){
        params.putString("deepLinkUrl", bundle.getString("deepLinkUrl"));
      }
      if (type != null && type.equals(PUSH_EVENT_NAME)){
        params.putString("event", bundle.getString("event"));
        params.putString("destination", bundle.getString("destination"));
        params.putString("data", bundle.getString("data"));
      }
      sendEvent(type, params);
    }
  }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    ReadySplash.show(this, true);// ⬅️ initialize the splash screen
    super.onCreate(savedInstanceState);
    mInstance = this;
    Bundle bundle = getIntent().getExtras();
    assert bundle != null;
    Bundle initProps = bundle.getBundle("initProps");
    mReactRootView = new ReactRootView(this);

    mReactInstanceManager = ReactInstanceManager.builder()
            .setApplication(getApplication())
            .setJavaScriptExecutorFactory(new HermesExecutorFactory())
            .setCurrentActivity(this)
            .setBundleAssetName("ReadyWallet.bundle")
            .setJSMainModulePath("index")
            .addPackages(getPackages())
            .setUseDeveloperSupport(false)
            .setInitialLifecycleState(LifecycleState.RESUMED)
            .build();
    mReactRootView.startReactApplication(mReactInstanceManager, "ReadyWallet", initProps);
    setContentView(mReactRootView);

    // Register the receiver
    registerReceiver(myReceiver, new IntentFilter(BROASCAST_EVENT));
    isLaunch= true;
}

// Use the context to send events
private void sendEvent(String type, WritableMap params) {
  mReactRootView.getReactInstanceManager().getCurrentReactContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
      .emit(type, params);
}