Android 如何从非React类发送React应用程序上下文?
我需要更新状态更改,比如android和react本机类之间的事件Android 如何从非React类发送React应用程序上下文?,android,ios,reactjs,react-native,bridge,Android,Ios,Reactjs,React Native,Bridge,我需要更新状态更改,比如android和react本机类之间的事件 public class MessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.d(TAG, "Push Message Received "); buil
public class MessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "Push Message Received ");
buildLocalNotification(remoteMessage);
}
public void buildLocalNotification(RemoteMessage message) {
...
FBMessagingHelper notificationHelper = new FBMessagingHelper(this.getApplication());
notificationHelper.showNotification(bundle);
}
}
当类扩展到FBMessingService时,如何创建ReactApplicationContext
构造函数
FBMessagingHelper notificationHelper = new FBMessagingHelper(this.getApplication());
// Getting Error as a parameter is not react app context
我需要更新视频进度中的状态
通过React Native
启动/停止视频时,更新到Android
通过Android
启动/停止视频时,更新到React Native
创建的本机模块:
public class FBMessagingPackage implements ReactPackage{
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new FBMessagingHelper(reactContext));
modules.add((NativeModule) new MessagingService(reactContext));
// Error: cannot be cast to nativeModule
return modules;
}
...
...
}
公共类FBMessagingPackage实现了ReactPackage{
@凌驾
公共列表createNativeModules(ReactApplicationContext reactContext){
列表模块=新的ArrayList();
添加(新的FBMessagingHelper(reactContext));
modules.add((NativeModule)new MessagingService(reactContext));
//错误:无法转换为nativeModule
返回模块;
}
...
...
}
我不知怎么解决了这个问题。我们无法在FirebaseMessagingService
上扩展ReactApplicationContext
。即使应用程序未运行且我们无法控制添加react上下文,也会调用此服务
因此,我创建了一个单独的模块并添加了监听器,这些监听器将更新事件数据到FBMessagingHelper类
public class FBMessagingPackage implements ReactPackage{
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new VideoModule(reactContext));
return modules;
}
}
public class FBMessagingPackage implements ReactPackage{
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new VideoModule(reactContext));
return modules;
}
}
public class VideoModule extends ReactContextBaseJavaModule {
private ReactApplicationContext mContext;
public VideoModule(ReactApplicationContext reactContext) { // Added into Native Modules
mContext = applicationContext;
}
@ReactMethod
public void onVideoProgress(Bolean status){
FBMessagingHelper.IN_VIDEO_PROGRESS = status
// Store it to public static variable of FBMessagingHelper
}
@Nonnull
@Override
public String getName() {
return "VideoModule";
}
}