react-native-fcm: Crash with ExpoKit

Hi ! I encountered so much problems integrating this library with expokit, but now, i’m really close of it ! I don’t have any problems with iOS but Android drives me mad.

When I receive an notification on Android, it’ll instantly crash with this error java.lang.NullPointerException: Attempt to invoke virtual method 'com.facebook.react.ReactInstanceManager com.facebook.react.ReactNativeHost.getReactInstanceManager()' on a null object reference

because of this line ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();

My MainApplication class is like this:

package com.productinuse.preprod;

import android.support.multidex.MultiDexApplication;

import com.facebook.react.ReactPackage;

import java.util.Arrays;
import java.util.List;

import com.evollu.react.fcm.FIRMessagingPackage;

public class MainApplication extends MultiDexApplication {
  public List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
        // Add your own packages here!
        new FIRMessagingPackage()
    );
  }
}

It doesn’t implement ReactApplication because of the ExpoKit implementation.

I already see this and this without any solutions

react-native-fcm: 7.5.1 expokit: sdk 19 @evollu I mention you because I’m really desperate aha 😉

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Comments: 15 (3 by maintainers)

Most upvoted comments

For others trying to configure it, I got it to work by changing MessagingService:

Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            public void run() {
                // Construct and load our normal React JS code bundle
                ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();

last line changed to :

ReactInstanceManager mReactInstanceManager = ((MainActivity)Exponent.getInstance().getCurrentActivity()).getReactInstanceManager();

Where I added a method in MainActivity (which extends ExponentActivity) like:

  public ReactInstanceManager getReactInstanceManager() {
    return (ReactInstanceManager) mReactInstanceManager.get();
  }

Not sure if this can be be done with PR or not.

@amanforindia Can you please share the imports used in your snippet? Currently, it highlights (MainActivity)Exponent in red for me, saying unable to resolve symbol

try if you can do it though some kind of reflection? I don’t know java much