Skip to content

Commit 3de55a0

Browse files
committed
fix enable sync
1 parent 828c9d1 commit 3de55a0

File tree

3 files changed

+119
-54
lines changed

3 files changed

+119
-54
lines changed

examples/demo/App.tsx

Lines changed: 94 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
import React, { useEffect } from 'react';
2-
import { View, Text, StyleSheet, StatusBar } from 'react-native';
3-
import Svg, { Path } from 'react-native-svg';
41
import { NavigationContainer } from '@react-navigation/native';
52
import { createNativeStackNavigator } from '@react-navigation/native-stack';
6-
import { SafeAreaProvider } from 'react-native-safe-area-context';
7-
import Toast from 'react-native-toast-message';
3+
import React, { useEffect } from 'react';
4+
import { StatusBar, StyleSheet, Text, View } from 'react-native';
85
import {
9-
OneSignal,
10-
LogLevel,
116
InAppMessageClickEvent,
12-
InAppMessageWillDisplayEvent,
7+
InAppMessageDidDismissEvent,
138
InAppMessageDidDisplayEvent,
149
InAppMessageWillDismissEvent,
15-
InAppMessageDidDismissEvent,
10+
InAppMessageWillDisplayEvent,
11+
LogLevel,
1612
NotificationClickEvent,
1713
NotificationWillDisplayEvent,
14+
OneSignal,
1815
} from 'react-native-onesignal';
16+
import { SafeAreaProvider } from 'react-native-safe-area-context';
17+
import Svg, { Path } from 'react-native-svg';
18+
import Toast from 'react-native-toast-message';
1919

2020
import { AppContextProvider } from './src/context/AppContext';
2121
import HomeScreen from './src/screens/HomeScreen';
2222
import SecondaryScreen from './src/screens/SecondaryScreen';
23-
import PreferencesService from './src/services/PreferencesService';
23+
import LogManager from './src/services/LogManager';
2424
import OneSignalApiService from './src/services/OneSignalApiService';
25+
import PreferencesService from './src/services/PreferencesService';
2526
import TooltipHelper from './src/services/TooltipHelper';
26-
import LogManager from './src/services/LogManager';
2727
import { Colors } from './src/theme';
2828

2929
const Stack = createNativeStackNavigator();
@@ -35,14 +35,19 @@ function App() {
3535
const init = async () => {
3636
try {
3737
const prefs = PreferencesService.getInstance();
38-
const [appId, consentRequired, privacyConsent, iamPaused, locationShared] =
39-
await Promise.all([
40-
prefs.getAppId(),
41-
prefs.getConsentRequired(),
42-
prefs.getPrivacyConsent(),
43-
prefs.getIamPaused(),
44-
prefs.getLocationShared(),
45-
]);
38+
const [
39+
appId,
40+
consentRequired,
41+
privacyConsent,
42+
iamPaused,
43+
locationShared,
44+
] = await Promise.all([
45+
prefs.getAppId(),
46+
prefs.getConsentRequired(),
47+
prefs.getPrivacyConsent(),
48+
prefs.getIamPaused(),
49+
prefs.getLocationShared(),
50+
]);
4651

4752
OneSignalApiService.getInstance().setAppId(appId);
4853

@@ -54,33 +59,67 @@ function App() {
5459
OneSignal.InAppMessages.setPaused(iamPaused);
5560
OneSignal.Location.setShared(locationShared);
5661

62+
console.log(
63+
'OneSignal.Notifications.getPermissionAsync: ',
64+
await OneSignal.Notifications.getPermissionAsync(),
65+
);
66+
5767
// Register SDK event listeners for logging
58-
OneSignal.InAppMessages.addEventListener('willDisplay', (e: InAppMessageWillDisplayEvent) => {
59-
log.i(TAG, `IAM willDisplay: ${e.message.messageId}`);
60-
});
61-
OneSignal.InAppMessages.addEventListener('didDisplay', (e: InAppMessageDidDisplayEvent) => {
62-
log.i(TAG, `IAM didDisplay: ${e.message.messageId}`);
63-
});
64-
OneSignal.InAppMessages.addEventListener('willDismiss', (e: InAppMessageWillDismissEvent) => {
65-
log.i(TAG, `IAM willDismiss: ${e.message.messageId}`);
66-
});
67-
OneSignal.InAppMessages.addEventListener('didDismiss', (e: InAppMessageDidDismissEvent) => {
68-
log.i(TAG, `IAM didDismiss: ${e.message.messageId}`);
69-
});
70-
OneSignal.InAppMessages.addEventListener('click', (e: InAppMessageClickEvent) => {
71-
log.i(TAG, `IAM click: ${e.result.actionId ?? 'unknown'}`);
72-
});
73-
OneSignal.Notifications.addEventListener('click', (e: NotificationClickEvent) => {
74-
log.i(TAG, `Notification click: ${e.notification.title ?? ''}`);
75-
});
76-
OneSignal.Notifications.addEventListener('permissionChange', (granted: boolean) => {
77-
log.i(TAG, `Permission changed: ${granted}`);
78-
});
79-
OneSignal.Notifications.addEventListener('foregroundWillDisplay', (e: NotificationWillDisplayEvent) => {
80-
log.i(TAG, `Notification foregroundWillDisplay: ${e.getNotification().title ?? ''}`);
81-
e.preventDefault();
82-
e.getNotification().display();
83-
});
68+
OneSignal.InAppMessages.addEventListener(
69+
'willDisplay',
70+
(e: InAppMessageWillDisplayEvent) => {
71+
log.i(TAG, `IAM willDisplay: ${e.message.messageId}`);
72+
},
73+
);
74+
OneSignal.InAppMessages.addEventListener(
75+
'didDisplay',
76+
(e: InAppMessageDidDisplayEvent) => {
77+
log.i(TAG, `IAM didDisplay: ${e.message.messageId}`);
78+
},
79+
);
80+
OneSignal.InAppMessages.addEventListener(
81+
'willDismiss',
82+
(e: InAppMessageWillDismissEvent) => {
83+
log.i(TAG, `IAM willDismiss: ${e.message.messageId}`);
84+
},
85+
);
86+
OneSignal.InAppMessages.addEventListener(
87+
'didDismiss',
88+
(e: InAppMessageDidDismissEvent) => {
89+
log.i(TAG, `IAM didDismiss: ${e.message.messageId}`);
90+
},
91+
);
92+
OneSignal.InAppMessages.addEventListener(
93+
'click',
94+
(e: InAppMessageClickEvent) => {
95+
log.i(TAG, `IAM click: ${e.result.actionId ?? 'unknown'}`);
96+
},
97+
);
98+
OneSignal.Notifications.addEventListener(
99+
'click',
100+
(e: NotificationClickEvent) => {
101+
log.i(TAG, `Notification click: ${e.notification.title ?? ''}`);
102+
},
103+
);
104+
OneSignal.Notifications.addEventListener(
105+
'permissionChange',
106+
(granted: boolean) => {
107+
log.i(TAG, `Permission changed: ${granted}`);
108+
},
109+
);
110+
OneSignal.Notifications.addEventListener(
111+
'foregroundWillDisplay',
112+
(e: NotificationWillDisplayEvent) => {
113+
log.i(
114+
TAG,
115+
`Notification foregroundWillDisplay: ${
116+
e.getNotification().title ?? ''
117+
}`,
118+
);
119+
e.preventDefault();
120+
e.getNotification().display();
121+
},
122+
);
84123

85124
log.i(TAG, `OneSignal initialized with app ID: ${appId}`);
86125
} catch (err) {
@@ -96,7 +135,10 @@ function App() {
96135

97136
return (
98137
<SafeAreaProvider>
99-
<StatusBar backgroundColor={Colors.oneSignalRed} barStyle="light-content" />
138+
<StatusBar
139+
backgroundColor={Colors.oneSignalRed}
140+
barStyle="light-content"
141+
/>
100142
<AppContextProvider>
101143
<NavigationContainer>
102144
<Stack.Navigator
@@ -112,7 +154,12 @@ function App() {
112154
options={{
113155
headerTitle: () => (
114156
<View style={headerStyles.container}>
115-
<Svg width={26} height={26} viewBox="0 0 70 70" style={headerStyles.logo}>
157+
<Svg
158+
width={26}
159+
height={26}
160+
viewBox="0 0 70 70"
161+
style={headerStyles.logo}
162+
>
116163
<Path
117164
d="M34.8106 0.345025C15.5675 0.383111 -0.107685 16.1805 0.000557241 35.4237C0.0499825 44.0258 3.27658 52.3062 9.06036 58.6738C14.8441 65.0414 22.7771 69.0471 31.3348 69.921C31.3907 69.9266 31.4472 69.9204 31.5006 69.9028C31.5539 69.8852 31.603 69.8565 31.6446 69.8187C31.6861 69.7809 31.7193 69.7348 31.7419 69.6834C31.7645 69.6319 31.7761 69.5763 31.7758 69.5201V35.2232H29.0657C28.9594 35.2232 28.8574 35.181 28.7822 35.1058C28.707 35.0306 28.6648 34.9286 28.6648 34.8223V29.4102C28.6648 29.3039 28.707 29.2019 28.7822 29.1267C28.8574 29.0515 28.9594 29.0093 29.0657 29.0093H37.5728C37.6791 29.0093 37.7811 29.0515 37.8563 29.1267C37.9314 29.2019 37.9737 29.3039 37.9737 29.4102V69.5201C37.9734 69.5763 37.9849 69.6319 38.0075 69.6834C38.0301 69.7348 38.0633 69.7809 38.1049 69.8187C38.1465 69.8565 38.1955 69.8852 38.2489 69.9028C38.3023 69.9204 38.3588 69.9266 38.4147 69.921C47.317 69.0121 55.5294 64.7164 61.3533 57.9223C67.1771 51.1281 70.1668 42.3555 69.7038 33.4189C69.2409 24.4823 65.3608 16.0654 58.8662 9.90943C52.3715 3.75341 43.7592 0.32918 34.8106 0.345025ZM44.7228 62.1516C44.6622 62.1731 44.5974 62.1798 44.5337 62.171C44.47 62.1622 44.4094 62.1382 44.3569 62.1011C44.3044 62.0639 44.2616 62.0147 44.2322 61.9575C44.2027 61.9004 44.1874 61.837 44.1876 61.7727V56.0479C44.1877 55.934 44.2202 55.8225 44.2812 55.7263C44.3423 55.6301 44.4294 55.5533 44.5324 55.5047C49.1635 53.3017 52.9013 49.5803 55.1247 44.959C57.3481 40.3377 57.9233 35.0947 56.7545 30.1013C55.5858 25.1079 52.7436 20.6648 48.7002 17.5102C44.6568 14.3557 39.6557 12.6797 34.528 12.7609C22.5972 12.9433 12.8073 22.5648 12.4284 34.4916C12.2897 38.8588 13.4263 43.1719 15.6992 46.9037C17.9722 50.6354 21.283 53.6242 25.2271 55.5047C25.3301 55.5533 25.4172 55.6301 25.4782 55.7263C25.5393 55.8225 25.5718 55.934 25.5719 56.0479V61.7727C25.5721 61.837 25.5568 61.9004 25.5273 61.9575C25.4979 62.0147 25.4551 62.0639 25.4026 62.1011C25.3501 62.1382 25.2895 62.1622 25.2258 62.171C25.1621 62.1798 25.0973 62.1731 25.0367 62.1516C19.4788 60.1122 14.6871 56.4048 11.3177 51.5369C7.94824 46.669 6.16583 40.8787 6.21449 34.9586C6.35079 19.3877 19.0753 6.67321 34.6522 6.55895C50.5659 6.43067 63.543 19.3396 63.543 35.2232C63.543 47.5749 55.6974 58.1265 44.7228 62.1516Z"
118165
fill="white"

examples/demo/src/context/AppContext.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,10 @@ export function AppContextProvider({ children }: Props) {
341341
OneSignalApiService.getInstance().setAppId(appId);
342342

343343
const externalId = await repository.getExternalId();
344-
const pushId = repository.getPushSubscriptionId();
345-
const pushOptedIn = repository.isPushOptedIn();
344+
const [pushId, pushOptedIn] = await Promise.all([
345+
repository.getPushSubscriptionIdAsync(),
346+
repository.isPushOptedInAsync(),
347+
]);
346348
const hasPerm = repository.hasPermission();
347349

348350
if (!mountedRef.current) {
@@ -384,15 +386,22 @@ export function AppContextProvider({ children }: Props) {
384386
}, [fetchUserDataFromApi]);
385387

386388
useEffect(() => {
387-
const pushSubHandler = () => {
389+
const pushSubHandler = async () => {
390+
if (!mountedRef.current) {
391+
return;
392+
}
393+
const [id, optedIn] = await Promise.all([
394+
repository.getPushSubscriptionIdAsync(),
395+
repository.isPushOptedInAsync(),
396+
]);
388397
if (!mountedRef.current) {
389398
return;
390399
}
391400
dispatch({
392401
type: 'SET_PUSH_SUBSCRIPTION',
393402
payload: {
394-
id: repository.getPushSubscriptionId(),
395-
optedIn: repository.isPushOptedIn(),
403+
id,
404+
optedIn,
396405
},
397406
});
398407
};

examples/demo/src/repositories/OneSignalRepository.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,19 @@ class OneSignalRepository {
100100
return id || undefined;
101101
}
102102

103+
async getPushSubscriptionIdAsync(): Promise<string | undefined> {
104+
const id = await OneSignal.User.pushSubscription.getIdAsync();
105+
return id ?? undefined;
106+
}
107+
103108
isPushOptedIn(): boolean {
104109
return OneSignal.User.pushSubscription.getOptedIn();
105110
}
106111

112+
async isPushOptedInAsync(): Promise<boolean> {
113+
return OneSignal.User.pushSubscription.getOptedInAsync();
114+
}
115+
107116
optInPush(): void {
108117
OneSignal.User.pushSubscription.optIn();
109118
}
@@ -157,15 +166,15 @@ class OneSignalRepository {
157166

158167
// Notification sending (via REST API)
159168
async sendNotification(type: NotificationType): Promise<boolean> {
160-
const subscriptionId = this.getPushSubscriptionId();
169+
const subscriptionId = await this.getPushSubscriptionIdAsync();
161170
if (!subscriptionId) {
162171
return false;
163172
}
164173
return this.apiService.sendNotification(type, subscriptionId);
165174
}
166175

167176
async sendCustomNotification(title: string, body: string): Promise<boolean> {
168-
const subscriptionId = this.getPushSubscriptionId();
177+
const subscriptionId = await this.getPushSubscriptionIdAsync();
169178
if (!subscriptionId) {
170179
return false;
171180
}

0 commit comments

Comments
 (0)