1

我无法从 java 活动中传递数据来做出原生反应。我正在处理信用卡付款,付款完成后,响应存储在一个名为 message 的变量中。我需要将此消息传递给我的反应本机代码。

// Java模块,我要传递的数据在“消息”中

公共类 HelloWorldModule 扩展 ReactContextBaseJavaModule 实现 ActivityEventListener{ Activity 活动;

反应应用上下文反应上下文;公共 HelloWorldModule(ReactApplicationContext reactContext,Activity 活动) { super(reactContext); //React Native 需要 this.reactContext= reactContext; this.activity=活动;reactContext.addActivityEventListener(this); //将此native模块注册为Activity结果监听器

}

@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

公共无效onNewIntent(意图意图){

}

@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());
    }                  
}

}

// 反应原生代码

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

请帮忙。

4

1 回答 1

0

使用设备发射器从本机发送数据以响应本机

在活动结果中添加以下代码

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

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

在反应原生添加潜水发射器监听器

import {DeviceEventEmitter} from 'react-native';

EmitterModule.addListener('message', (message) => {
  console.log(message);
};
于 2020-02-28T09:13:12.473 回答