@@ -104,6 +104,9 @@ public class RNOneSignal extends ReactContextBaseJavaModule implements
104104 private boolean hasAddedNotificationClickListener = false ;
105105 private boolean hasAddedInAppMessageClickListener = false ;
106106
107+ // Static reference to track current instance for cleanup on reload
108+ private static RNOneSignal currentInstance = null ;
109+
107110 private IInAppMessageClickListener rnInAppClickListener = new IInAppMessageClickListener () {
108111 @ Override
109112 public void onClick (IInAppMessageClickEvent event ) {
@@ -171,19 +174,22 @@ private void removeObservers() {
171174 }
172175
173176 private void removeHandlers () {
174- if (!oneSignalInitDone ) {
175- Logging .debug ("OneSignal React-Native SDK not initialized yet. Could not remove handlers." , null );
176- return ;
177+ if (hasAddedInAppMessageClickListener ) {
178+ OneSignal .getInAppMessages ().removeClickListener (rnInAppClickListener );
179+ hasAddedInAppMessageClickListener = false ;
180+ }
181+ if (hasAddedInAppMessageLifecycleListener ) {
182+ OneSignal .getInAppMessages ().removeLifecycleListener (rnInAppLifecycleListener );
183+ hasAddedInAppMessageLifecycleListener = false ;
184+ }
185+ if (hasAddedNotificationClickListener ) {
186+ OneSignal .getNotifications ().removeClickListener (rnNotificationClickListener );
187+ hasAddedNotificationClickListener = false ;
188+ }
189+ if (hasAddedNotificationForegroundListener ) {
190+ OneSignal .getNotifications ().removeForegroundLifecycleListener (this );
191+ hasAddedNotificationForegroundListener = false ;
177192 }
178-
179- OneSignal .getInAppMessages ().removeClickListener (rnInAppClickListener );
180- hasAddedInAppMessageClickListener = false ;
181- OneSignal .getInAppMessages ().removeLifecycleListener (rnInAppLifecycleListener );
182- hasAddedInAppMessageLifecycleListener = false ;
183- OneSignal .getNotifications ().removeClickListener (rnNotificationClickListener );
184- hasAddedNotificationClickListener = false ;
185- OneSignal .getNotifications ().removeForegroundLifecycleListener (this );
186- hasAddedNotificationForegroundListener = false ;
187193 }
188194
189195 private void sendEvent (String eventName , Object params ) {
@@ -197,6 +203,13 @@ public RNOneSignal(ReactApplicationContext reactContext) {
197203 mReactContext .addLifecycleEventListener (this );
198204 notificationWillDisplayCache = new HashMap <String , INotificationWillDisplayEvent >();
199205 preventDefaultCache = new HashMap <String , INotificationWillDisplayEvent >();
206+
207+ // Clean up previous instance if it exists (handles reload scenario)
208+ if (currentInstance != null && currentInstance != this ) {
209+ currentInstance .removeHandlers ();
210+ currentInstance .removeObservers ();
211+ }
212+ currentInstance = this ;
200213 }
201214
202215
@@ -549,7 +562,7 @@ public void onPushSubscriptionChange(PushSubscriptionChangedState pushSubscripti
549562 Logging .debug ("Sending subscription change event" , null );
550563 } catch (JSONException e ) {
551564 e .printStackTrace ();
552- }
565+ }
553566 }
554567
555568 @ ReactMethod
@@ -619,7 +632,7 @@ public void getTags(Promise promise) {
619632 for (Map .Entry <String , String > entry : tags .entrySet ()) {
620633 writableTags .putString (entry .getKey (), entry .getValue ());
621634 }
622- promise .resolve (writableTags );
635+ promise .resolve (writableTags );
623636 }
624637
625638 @ ReactMethod
@@ -718,7 +731,7 @@ public void onUserStateChange(UserChangedState state) {
718731 Logging .debug ("Sending user state change event" , null );
719732 } catch (JSONException e ) {
720733 e .printStackTrace ();
721- }
734+ }
722735 }
723736
724737 @ ReactMethod
0 commit comments