簡體   English   中英

如何從非反應 class 發送反應應用程序上下文?

[英]How do I send React Application context from non react 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);
      }
    }

當 class 擴展到 FBMessingService 時,如何創建ReactApplicationContext構造函數?

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
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 即使應用程序未運行並且我們無法控制添加反應上下文,也會調用此服務。

因此,我創建了一個單獨的模塊並添加了將事件數據更新到 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM