diff --git a/lib/android/app/src/main/java/com/wix/reactnativenotifications/fcm/FcmToken.java b/lib/android/app/src/main/java/com/wix/reactnativenotifications/fcm/FcmToken.java index fb1aba4c7..3b1a61125 100644 --- a/lib/android/app/src/main/java/com/wix/reactnativenotifications/fcm/FcmToken.java +++ b/lib/android/app/src/main/java/com/wix/reactnativenotifications/fcm/FcmToken.java @@ -88,12 +88,24 @@ protected void refreshToken() { } protected void sendTokenToJS() { - final ReactInstanceManager instanceManager = ((ReactApplication) mAppContext).getReactNativeHost().getReactInstanceManager(); - ReactContext reactContext = instanceManager.getCurrentReactContext(); + ReactContext reactContext = null; - if (reactContext == null) { - // If the react context is not available, try to get the current context from the react host (RN0.76). + // Try New Architecture first (ReactHost) - available in RN 0.76+ + try { reactContext = ((ReactApplication) mAppContext).getReactHost().getCurrentReactContext(); + } catch (NoSuchMethodError | RuntimeException e) { + // getReactHost() doesn't exist in older RN versions or throws in some cases + // Fall back to Old Architecture (ReactNativeHost) + } + + if (reactContext == null) { + try { + final ReactInstanceManager instanceManager = ((ReactApplication) mAppContext).getReactNativeHost().getReactInstanceManager(); + reactContext = instanceManager.getCurrentReactContext(); + } catch (RuntimeException e) { + // getReactNativeHost() throws RuntimeException in New Architecture + // This is expected, we'll just continue with null reactContext + } } // Note: Cannot assume react-context exists cause this is an async dispatched service. if (reactContext != null && reactContext.hasActiveReactInstance()) { diff --git a/react-native.config.js b/react-native.config.js index d246b0c85..bb1e4a344 100644 --- a/react-native.config.js +++ b/react-native.config.js @@ -4,7 +4,7 @@ module.exports = { ios: {}, android: { sourceDir: './lib/android/app', - packageInstance: 'new RNNotificationsPackage(reactNativeHost.getApplication())', + packageInstance: 'new RNNotificationsPackage(getApplication())', } }, },