Skip to content

Commit 9188243

Browse files
committed
fix removing of listeners on app reload
1 parent 8aac00a commit 9188243

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

examples/RNOneSignalTS/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2665,7 +2665,7 @@ SPEC CHECKSUMS:
26652665
React-logger: a3cb5b29c32b8e447b5a96919340e89334062b48
26662666
React-Mapbuffer: 9d2434a42701d6144ca18f0ca1c4507808ca7696
26672667
React-microtasksnativemodule: 75b6604b667d297292345302cc5bfb6b6aeccc1b
2668-
react-native-onesignal: 6c5758aa56975db4bca9956d18d83dd62444c931
2668+
react-native-onesignal: b68c981956150f288c1585889871affcef3c0b8b
26692669
react-native-safe-area-context: c00143b4823773bba23f2f19f85663ae89ceb460
26702670
React-NativeModulesApple: 879fbdc5dcff7136abceb7880fe8a2022a1bd7c3
26712671
React-oscompat: 93b5535ea7f7dff46aaee4f78309a70979bdde9d

0 commit comments

Comments
 (0)