简体   繁体   English

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

[英]How do I send React Application context from non react class?

I need to update status changes like event between android and react native class?我需要更新状态更改,例如 android 之间的事件并反应原生 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);
      }
    }

How can I create a ReactApplicationContext constructor as class is extended to FBMessingService?当 class 扩展到 FBMessingService 时,如何创建ReactApplicationContext构造函数?

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

I need to update IN_VIDEO_PROGRESS status我需要更新IN_VIDEO_PROGRESS状态

  1. Update to Android when video starts/stop by React Native .React Native启动/停止视频时,更新到Android
  2. Update to React Native when video starts/stop by Android .当视频由Android开始/停止时更新为React Native
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
    }
  }
}

Created Native Modules:创建的本机模块:

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

I have somehow solved this.我以某种方式解决了这个问题。 we cannot extend ReactApplicationContext on FirebaseMessagingService .我们不能在FirebaseMessagingService上扩展ReactApplicationContext This service will be called even when app is not running and we don't have control to add react context.即使应用程序未运行并且我们无法控制添加反应上下文,也会调用此服务。

So I have created a separate module and added the listeners which will update the event data to FBMessagingHelper class.因此,我创建了一个单独的模块并添加了将事件数据更新到 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";
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从“非图形”类获取应用程序上下文 - How to get the application context from a “non-graphic” class Android:如何从扩展应用程序的 class 将应用程序上下文作为活动获取? - Android: How do I get the Application Context as an Activity from a class that extends Application? 如何获取模块中没有应用程序类的非活动类的上下文 - How to get context of a non activity class with no Application Class in a module 从非活动单例类中获取应用程序上下文 - Get application context from non activity singleton class 如何从活动类中获取上下文? - How do I get the context from within an activity class? 如何在 Expo 客户端上打开 React Native 应用程序? - How do I open a React Native Application on Expo Client? 如何将数据从在 kotlin 上运行的 android 应用程序发送到在 webview 上打开的 react js 应用程序? - how to send data from android application running on kotlin to a react js application which is opened on webview? 如何从非活动类开始新的活动 - How do I start a new Activity from non Activity class 我如何在非活动课上敬酒? - How do I make a toast from a non activity class? 如何从非活动类开始活动? - How do I start an Activity from a non-Activity class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM