Search code examples
androidreact-nativenative-modulereact-native-native-module

how to Send object/string from android Activity onActivityResult to react native


In my react native project ,I have implemented payment native sdk in android (that does not supported in react native).so i am tried to call native sdk with native modules..

i am able calling the payment SDKUI from react native native module ,but when the time of results can not send results back to react native component..

Payment gateway is -> PAYUBIZ

for more details pls find below code..

at the end of payment gateway i have displayed payment response in android native alert..

Code used..

1. Created NATIVE MODULES in react native side..



     import {NativeModules} from 'react-native';
        module.exports = NativeModules.PayUBizAccess;

        in button action following code to call native method from android
        PayUBizAccess.showPayuBiz();

2. Created ReactContextBaseJavaModule based PayUBizModule



@ReactMethod
  public void showPayuBiz() {

    final Activity activity = getCurrentActivity();


    Intent intent = new Intent(activity, PayuActivity.class);


    getReactApplicationContext().startActivity(intent);
   }

PayuActivity.class is the payment activity class

3. Display results after payment success or failure..



     @Override
            public void onActivityResult(int requestCode, int resultCode, final Intent data) {



                if (requestCode == PayuConstants.PAYU_REQUEST_CODE) {
                    if (data != null) {


                        new AlertDialog.Builder(this)
                                .setCancelable(false)
                                .setMessage("Payu's Data : " + data.getStringExtra("payu_response") + "\n\n\n Merchant's Data: " + data.getStringExtra("result"))
                                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        dialog.dismiss();
                                        finish();
                                    }
                                }).show();

                    } else {
                        Toast.makeText(this, getString(R.string.could_not_receive_data), Toast.LENGTH_LONG).show();
                    }
                }
            }

4. After alert clicking button in alert it directly moves to react native component..

So now i want results data's to react native ,Kindly suggest me any solutions

thanks in advance


Solution

  • You can send an event from your native code like this:

    private void sendEvent(ReactContext reactContext,
                       String eventName,
                       @Nullable WritableMap params) {
      reactContext
        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
        //supply the result in params
        .emit(eventName, params);
    }
    

    And in your react native code you can receive the event like this:

    componentWillMount: function() {
       DeviceEventEmitter.addListener('keyboardWillShow', function(e: Event) {
      // handle event.
    });
    

    }

    Check full docs here

    Another way of doing this is given here