1
votes

I am unable to pass data from java activity to react native. I am processing a card payment, and when the payment is done, the response is stored in a variable called message. I need to pass this message to my react native code.

// Java module, the data I want to pass is in "message"

public class HelloWorldModule extends ReactContextBaseJavaModule implements ActivityEventListener{ Activity activity;

ReactApplicationContext reactContext; public HelloWorldModule(ReactApplicationContext reactContext,Activity activity) { super(reactContext); //required by React Native this.reactContext= reactContext; this.activity= activity; reactContext.addActivityEventListener(this); //Register this native module as Activity result listener

}

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
    // ReactApplicationContext reactContext = this.getReactNativeHost().getReactInstanceManager().getCurrentReactApplicationContext();
    // reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit();
    // reactContext
    // .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
    // .emit('message', message);

/*
 *  We advise you to do a further verification of transaction's details on your server to be
 *  sure everything checks out before providing service or goods.
*/
if (requestCode == RaveConstants.RAVE_REQUEST_CODE && data != null) {
    String message = data.getStringExtra("response");
    // Log.e("RAVE",message);
    if (resultCode == RavePayActivity.RESULT_SUCCESS) {
        Toast.makeText(activity, "SUCCESS " + message, Toast.LENGTH_SHORT).show();
    }
    else if (resultCode == RavePayActivity.RESULT_ERROR) {
        Toast.makeText(activity, "ERROR " + message, Toast.LENGTH_SHORT).show();
    }
    else if (resultCode == RavePayActivity.RESULT_CANCELLED) {
        Toast.makeText(activity, "CANCELLED " + message, Toast.LENGTH_SHORT).show();
    }
}
// else {
//      super.onActivityResult(activity, requestCode, resultCode, data);
// }

}

// @Override // public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { // Toast.makeText( activity , "hello", Toast.LENGTH_SHORT).show(); // }

@Override

public void onNewIntent(Intent intent) {

}

@Override
//getName is required to define the name of the module represented in JavaScript
public String getName() { 
    return "HelloWorld";
}

@ReactMethod
public void sayHi(Callback errorCallback, Callback successCallback) {
    try{
        int amount = 30;//call.argument("amount");
                    String narration = "Payment for soup";//call.argument("nara");
                    String countryCode = "NG"; //call.argument("countryCode");
                    String currency =  "NGN"; //call.argument("currency");
                    String amountText = "50";//call.argument("amountText");
                    String email = "*****@yahoo.com";//call.argument("email");
                    String name = "Ubanna Danny";//call.argument("name");
                    String paymentId = "a98sjkhdjdu";//call.argument("paymentId");



                    String key ="FLWPUBK-****-X";
                     String encryptionKey = "****";

                            new RavePayManager(activity).setAmount(Double.parseDouble(String.valueOf(amount)))
                            .setCountry(countryCode)
                            .setCurrency(currency)
                            .setEmail(email)
                            .setfName(name)
                            .setlName("")
                            .setNarration(narration)
                            .setPublicKey(key)
                            .setEncryptionKey(encryptionKey)
                            .setTxRef(paymentId)
                            .acceptMpesaPayments(false)
                            .acceptAccountPayments(true)
                            .acceptCardPayments(true)
                            .acceptGHMobileMoneyPayments(false)
                            .onStagingEnv(false)
                            .allowSaveCardFeature(true)
                            .initialize();
    } catch (IllegalViewOperationException e) {
        errorCallback.invoke(e.getMessage());
    }                  
}

}

// React native code

   // async function to call the Java native method
    async sayHiFromJava() {
      HelloWorld.sayHi( (err) => {console.log(err)}, (msg) => {console.log(msg)} );
    }

Please help.

1

1 Answers

0
votes

use device emitter to send data from native to react native

in on activity result add following code

ReactContext context = this.getReactNativeHost().getReactInstanceManager().getCurrentReactContext();

context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit();
context
  .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  .emit('message', message);

in react native add Dives emiter listener

import {DeviceEventEmitter} from 'react-native';

EmitterModule.addListener('message', (message) => {
  console.log(message);
};