Skip to content

Commit 1fb1278

Browse files
committed
wip
1 parent 8aac00a commit 1fb1278

File tree

3 files changed

+56
-85
lines changed

3 files changed

+56
-85
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/OSDemo.tsx

Lines changed: 27 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
TextInput,
99
View,
1010
} from 'react-native';
11-
import { LogLevel, OneSignal } from 'react-native-onesignal';
11+
import {
12+
LogLevel,
13+
NotificationWillDisplayEvent,
14+
OneSignal,
15+
} from 'react-native-onesignal';
1216
import { renderButtonView } from './Helpers';
1317
import OSButtons from './OSButtons';
1418
import OSConsole from './OSConsole';
@@ -134,86 +138,40 @@ const OSDemo: React.FC = () => {
134138
[OSLog],
135139
);
136140

141+
const handleForegroundNotification = useCallback(
142+
(event: NotificationWillDisplayEvent) => {
143+
const notification = event.getNotification();
144+
console.log('Arrived', notification);
145+
},
146+
[],
147+
);
148+
137149
useEffect(() => {
138-
const initializeOneSignal = async () => {
150+
const setup = async () => {
151+
console.log('Issue-1610-11');
152+
OneSignal.Debug.setLogLevel(LogLevel.Verbose);
139153
OneSignal.initialize(APP_ID);
140-
OneSignal.Debug.setLogLevel(LogLevel.None);
141-
142-
OneSignal.LiveActivities.setupDefault();
143-
// OneSignal.LiveActivities.setupDefault({
144-
// enablePushToStart: false,
145-
// enablePushToUpdate: true,
146-
// });
147-
148-
OneSignal.Notifications.addEventListener(
149-
'foregroundWillDisplay',
150-
onForegroundWillDisplay,
151-
);
152-
OneSignal.Notifications.addEventListener('click', onNotificationClick);
153-
OneSignal.InAppMessages.addEventListener('click', onIAMClick);
154-
OneSignal.InAppMessages.addEventListener('willDisplay', onIAMWillDisplay);
155-
OneSignal.InAppMessages.addEventListener('didDisplay', onIAMDidDisplay);
156-
OneSignal.InAppMessages.addEventListener('willDismiss', onIAMWillDismiss);
157-
OneSignal.InAppMessages.addEventListener('didDismiss', onIAMDidDismiss);
158-
OneSignal.User.pushSubscription.addEventListener(
159-
'change',
160-
onSubscriptionChange,
154+
OneSignal.Notifications.requestPermission(false);
155+
console.log('OneSignal ID:', await OneSignal.User.getOnesignalId());
156+
console.log('External ID:', await OneSignal.User.getExternalId());
157+
console.log(
158+
'Permission:',
159+
await OneSignal.User.pushSubscription.getIdAsync(),
161160
);
162161
OneSignal.Notifications.addEventListener(
163-
'permissionChange',
164-
onPermissionChange,
162+
'foregroundWillDisplay',
163+
handleForegroundNotification,
165164
);
166-
OneSignal.User.addEventListener('change', onUserChange);
167165
};
168-
169-
initializeOneSignal();
166+
setup();
170167

171168
return () => {
172-
// Clean up all event listeners
173169
OneSignal.Notifications.removeEventListener(
174170
'foregroundWillDisplay',
175-
onForegroundWillDisplay,
176-
);
177-
OneSignal.Notifications.removeEventListener('click', onNotificationClick);
178-
OneSignal.InAppMessages.removeEventListener('click', onIAMClick);
179-
OneSignal.InAppMessages.removeEventListener(
180-
'willDisplay',
181-
onIAMWillDisplay,
182-
);
183-
OneSignal.InAppMessages.removeEventListener(
184-
'didDisplay',
185-
onIAMDidDisplay,
186-
);
187-
OneSignal.InAppMessages.removeEventListener(
188-
'willDismiss',
189-
onIAMWillDismiss,
190-
);
191-
OneSignal.InAppMessages.removeEventListener(
192-
'didDismiss',
193-
onIAMDidDismiss,
194-
);
195-
OneSignal.User.pushSubscription.removeEventListener(
196-
'change',
197-
onSubscriptionChange,
198-
);
199-
OneSignal.Notifications.removeEventListener(
200-
'permissionChange',
201-
onPermissionChange,
171+
handleForegroundNotification,
202172
);
203-
OneSignal.User.removeEventListener('change', onUserChange);
204173
};
205-
}, [
206-
onForegroundWillDisplay,
207-
onNotificationClick,
208-
onIAMClick,
209-
onIAMWillDisplay,
210-
onIAMDidDisplay,
211-
onIAMWillDismiss,
212-
onIAMDidDismiss,
213-
onSubscriptionChange,
214-
onPermissionChange,
215-
onUserChange,
216-
]);
174+
}, [handleForegroundNotification]);
217175

218176
const inputChange = useCallback((text: string) => {
219177
setInputValue(text);

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)