我无法传递Java活动中的数据来响应本机。
我正在处理信用卡付款,付款完成后,响应存储在名为message的变量中。我需要将此消息传递给我的本机代码。
// Java模块,我要传递的数据在“消息”中
公共类HelloWorldModule扩展了ReactContextBaseJavaModule实现的ActivityEventListener {
活动活动;
ReactApplicationContext reactContext;
公共HelloWorldModule(ReactApplicationContext reactContext,Activity活动){
超级(reactContext); // React Native要求
this.reactContext = reactContext;
this.activity =活动;
reactContext.addActivityEventListener(this); //将此本地模块注册为活动结果侦听器
}
@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(活动活动,int requestCode,int resultCode,Intent数据){
// 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());
}
}
}
//反应本机代码
// async function to call the Java native method
async sayHiFromJava() {
HelloWorld.sayHi( (err) => {console.log(err)}, (msg) => {console.log(msg)} );
}
请帮忙。
最佳答案
使用设备发射器从本地发送数据以响应本地
在活动结果中添加以下代码
ReactContext context = this.getReactNativeHost().getReactInstanceManager().getCurrentReactContext();
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit();
context
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit('message', message);
在反应本机添加Dives发射器侦听器
import {DeviceEventEmitter} from 'react-native';
EmitterModule.addListener('message', (message) => {
console.log(message);
};