android - 如何从非 react 类发送 react 应用程序上下文?

标签 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 ");
        buildLocalNotification(remoteMessage);
      }

      public void buildLocalNotification(RemoteMessage message) {
        ...
        FBMessagingHelper notificationHelper = new FBMessagingHelper(this.getApplication());
        notificationHelper.showNotification(bundle);
      }
    }
如何创建 ReactApplicationContext作为类的构造函数扩展为 FBMessingService?
FBMessagingHelper notificationHelper = new FBMessagingHelper(this.getApplication());
// Getting Error as a parameter is not react app context
我需要更新 IN_VIDEO_PROGRESS地位
  • 更新至 Android视频何时开始/停止 React Native .
  • 更新至 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
        }
      }
    }
    
    创建的 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;
          }
            ...
            ...
        }
    

    最佳答案

    我以某种方式解决了这个问题。我们不能延长 ReactApplicationContextFirebaseMessagingService .即使应用程序未运行并且我们无法控制添加 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 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";
        }
    
    
    }
    

    关于android - 如何从非 react 类发送 react 应用程序上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63303616/

    相关文章:

    android - 检测传入的 SMS 消息

    ios - 由于闭包强引用或 NSTimer,我的 Swift 代码未调用 deinit。我该如何解决?

    reactjs - 使用 grpc 流在反序列化响应时失败

    javascript - 子组件调用父组件函数

    iphone - 隐藏有关缺少体系结构信息的 Xcode dsymutil 警告

    javascript - 将 setState 与父元素的 props 一起使用

    android - 使用 ActionBarSherlock 跨 fragment 使用一个通用按钮

    java - 如何保持列表不 fragment 化(阅读解释)

    android - HighScore 列表 gui 问题

    ios - Xcode 特征 View 不为零