@@ -96,6 +96,9 @@ public class RNOneSignal extends ReactContextBaseJavaModule
9696 private boolean hasAddedNotificationClickListener = false ;
9797 private boolean hasAddedInAppMessageClickListener = false ;
9898
99+ // Static reference to track current instance for cleanup on reload
100+ private static RNOneSignal currentInstance = null ;
101+
99102 private IInAppMessageClickListener rnInAppClickListener = new IInAppMessageClickListener () {
100103 @ Override
101104 public void onClick (IInAppMessageClickEvent event ) {
@@ -175,19 +178,22 @@ private void removeObservers() {
175178 }
176179
177180 private void removeHandlers () {
178- if (!oneSignalInitDone ) {
179- Logging .debug ("OneSignal React-Native SDK not initialized yet. Could not remove handlers." , null );
180- return ;
181+ if (hasAddedInAppMessageClickListener ) {
182+ OneSignal .getInAppMessages ().removeClickListener (rnInAppClickListener );
183+ hasAddedInAppMessageClickListener = false ;
184+ }
185+ if (hasAddedInAppMessageLifecycleListener ) {
186+ OneSignal .getInAppMessages ().removeLifecycleListener (rnInAppLifecycleListener );
187+ hasAddedInAppMessageLifecycleListener = false ;
188+ }
189+ if (hasAddedNotificationClickListener ) {
190+ OneSignal .getNotifications ().removeClickListener (rnNotificationClickListener );
191+ hasAddedNotificationClickListener = false ;
192+ }
193+ if (hasAddedNotificationForegroundListener ) {
194+ OneSignal .getNotifications ().removeForegroundLifecycleListener (this );
195+ hasAddedNotificationForegroundListener = false ;
181196 }
182-
183- OneSignal .getInAppMessages ().removeClickListener (rnInAppClickListener );
184- hasAddedInAppMessageClickListener = false ;
185- OneSignal .getInAppMessages ().removeLifecycleListener (rnInAppLifecycleListener );
186- hasAddedInAppMessageLifecycleListener = false ;
187- OneSignal .getNotifications ().removeClickListener (rnNotificationClickListener );
188- hasAddedNotificationClickListener = false ;
189- OneSignal .getNotifications ().removeForegroundLifecycleListener (this );
190- hasAddedNotificationForegroundListener = false ;
191197 }
192198
193199 private void sendEvent (String eventName , Object params ) {
@@ -203,6 +209,13 @@ public RNOneSignal(ReactApplicationContext reactContext) {
203209 mReactContext .addLifecycleEventListener (this );
204210 notificationWillDisplayCache = new HashMap <String , INotificationWillDisplayEvent >();
205211 preventDefaultCache = new HashMap <String , INotificationWillDisplayEvent >();
212+
213+ // Clean up previous instance if it exists (handles reload scenario)
214+ if (currentInstance != null && currentInstance != this ) {
215+ currentInstance .removeHandlers ();
216+ currentInstance .removeObservers ();
217+ }
218+ currentInstance = this ;
206219 }
207220
208221 /** Native Module Overrides */
0 commit comments