Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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?

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

I need to update IN_VIDEO_PROGRESS status

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

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;
      }
        ...
        ...
    }
like image 577
Balasubramanian Avatar asked Aug 07 '20 14:08

Balasubramanian


People also ask

Can we use context in class component?

Consuming Context With Class-based Components We have already seen a demo of how we can use the power of Context in class based components. One is to use the context from Consumer like “ThemeContext. Consumer” and the other method is by assigning context object from current Context to contextType property of our class.

What is createContext?

createContext lets you create a context that components can provide or read. const SomeContext = createContext(defaultValue) Usage. Creating context. Importing and exporting context from a file.


1 Answers

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.

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


}
like image 89
Balasubramanian Avatar answered Oct 21 '22 16:10

Balasubramanian