如何从非React类发送React Application上下文? [英] How do I send React Application context from non react class?

查看:214
本文介绍了如何从非React类发送React Application上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更新状态更改(例如android之间的事件)并响应本机类吗?

I need to update status changes like event between android and react native class?

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构造函数?

How can I create a ReactApplicationContext constructor as class is extended to FBMessingService?

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

我需要更新IN_VIDEO_PROGRESS状态

  1. 在视频按React Native开始/停止播放时更新为Android.
  2. 在视频按Android开始/停止播放时更新为React Native.
  1. Update to Android when video starts/stop by React Native.
  2. Update to React Native when video starts/stop by 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
    }
  }
}

创建的本机模块:

    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;
      }
        ...
        ...
    }

推荐答案

我已经以某种方式解决了这个问题.我们无法在FirebaseMessagingService上扩展ReactApplicationContext.即使应用未运行且我们无权添加反应上下文,该服务也会被调用.

I have somehow solved this. we cannot extend ReactApplicationContext on FirebaseMessagingService. This service will be called even when app is not running and we don't have control to add react context.

因此,我创建了一个单独的模块并添加了侦听器,这些侦听器会将事件数据更新到FBMessagingHelper类.

So I have created a separate module and added the listeners which will update the event data to FBMessagingHelper class.

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";
    }


}

这篇关于如何从非React类发送React Application上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆