From 24d9eaa0c906bbef9e2dd72406d6351ccf54b42f Mon Sep 17 00:00:00 2001 From: Enrico Mazzucchelli Date: Wed, 18 Sep 2024 19:46:17 +0200 Subject: [PATCH] Add check to verify that catalystInstance is active before require module. Remove duplicated code. --- .../wix/reactnativenotifications/core/JsIOHelper.java | 8 ++------ .../com/wix/reactnativenotifications/fcm/FcmToken.java | 10 ++++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/JsIOHelper.java b/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/JsIOHelper.java index 4d8f4d1d5..665a702c7 100644 --- a/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/JsIOHelper.java +++ b/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/JsIOHelper.java @@ -9,15 +9,11 @@ public class JsIOHelper { public boolean sendEventToJS(String eventName, Bundle data, ReactContext reactContext) { - if (reactContext != null) { - sendEventToJS(eventName, Arguments.fromBundle(data), reactContext); - return true; - } - return false; + return sendEventToJS(eventName, Arguments.fromBundle(data), reactContext); } public boolean sendEventToJS(String eventName, WritableMap data, ReactContext reactContext) { - if (reactContext != null) { + if (reactContext != null && reactContext.hasActiveCatalystInstance()) { reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, data); return true; } 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 7db6e8d88..1c3f9c2a8 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 @@ -90,12 +90,10 @@ protected void refreshToken() { protected void sendTokenToJS() { final ReactInstanceManager instanceManager = ((ReactApplication) mAppContext).getReactNativeHost().getReactInstanceManager(); final ReactContext reactContext = instanceManager.getCurrentReactContext(); + Bundle tokenMap = new Bundle(); + tokenMap.putString("deviceToken", sToken); + // mJsIOHelper is safe now when context is null or react instance is not active + mJsIOHelper.sendEventToJS(TOKEN_RECEIVED_EVENT_NAME, tokenMap, reactContext); - // Note: Cannot assume react-context exists cause this is an async dispatched service. - if (reactContext != null && reactContext.hasActiveCatalystInstance()) { - Bundle tokenMap = new Bundle(); - tokenMap.putString("deviceToken", sToken); - mJsIOHelper.sendEventToJS(TOKEN_RECEIVED_EVENT_NAME, tokenMap, reactContext); - } } }