如何从非反应类发送反应应用程序上下文?

Bal*_*ian 5 android bridge ios reactjs react-native

我需要更新状态更改,例如 android 和 react 本机类之间的事件?

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);
      }
    }
Run Code Online (Sandbox Code Playgroud)

ReactApplicationContext当类扩展到 FBMessingService 时,如何创建构造函数?

FBMessagingHelper notificationHelper = new FBMessagingHelper(this.getApplication());
// Getting Error as a parameter is not react app context
Run Code Online (Sandbox Code Playgroud)

我需要更新IN_VIDEO_PROGRESS状态

  1. 更新到Android视频开始/停止时间React Native
  2. 更新到React Native视频开始/停止时间Android
public class FBMessagingHelper extends ReactContextBaseJavaModule {
  private ReactApplicationContext mContext;
  private static boolean IN_VIDEO_PROGRESS = false;

  public FBMessagingHelper(ReactApplicationContext reactContext) { // Added into Native Modules
     mContext = applicationContext;
  }

@ReactMethod
    public void onVideoProgress(Bolean status){
        // ==== on videoProgress ====
        IN_VIDEO_PROGRESS = status
    }

 @Nonnull
 @Override
    public String getName() {
        return "FBMessagingHelper";
    }

  public void showCustomNotification(Bundle bundle) {
    if (!IN_VIDEO_PROGRESS && bundle.getString("category").equals("VIDEO") {
       IN_VIDEO_PROGRESS = true;
       // start FullScreenNotificationActivity
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

创建的原生模块:

    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;
      }
        ...
        ...
    }
Run Code Online (Sandbox Code Playgroud)

Bal*_*ian 3

我已经以某种方式解决了这个问题。我们不能继续ReactApplicationContext下去FirebaseMessagingService。即使应用程序未运行并且我们无法控制添加反应上下文,该服务也会被调用。

因此,我创建了一个单独的模块并添加了侦听器,这些侦听器将事件数据更新到 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;
      }
    }
Run Code Online (Sandbox Code Playgroud)
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";
    }


}
Run Code Online (Sandbox Code Playgroud)