Skip to content

Commit 1b6ea84

Browse files
committed
refactor(demo): source app ID from env vars
1 parent af1e632 commit 1b6ea84

8 files changed

Lines changed: 26 additions & 28 deletions

File tree

examples/demo/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
VITE_ONESIGNAL_APP_ID=your_onesignal_app_id
22
VITE_ONESIGNAL_API_KEY=your_rest_api_key
3+
VITE_ONESIGNAL_ANDROID_CHANNEL_ID=
34
VITE_E2E_MODE=false

examples/demo/src/hooks/useOneSignal.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import { NotificationType } from '../models/NotificationType';
1515
import OneSignalApiService, { API_KEY } from '../services/OneSignalApiService';
1616
import PreferencesService from '../services/PreferencesService';
1717

18+
const APP_ID = import.meta.env.VITE_ONESIGNAL_APP_ID as string | undefined;
19+
const DEFAULT_APP_ID = '77e32082-ea27-42e3-a898-c72e141824ef';
20+
21+
function resolveAppId(): string {
22+
return APP_ID?.trim() || DEFAULT_APP_ID;
23+
}
24+
1825
const apiService = OneSignalApiService.getInstance();
1926
const preferences = new PreferencesService();
2027

@@ -101,7 +108,7 @@ export type UseOneSignalReturn = {
101108
};
102109

103110
export function useOneSignal(): UseOneSignalReturn {
104-
const [appId, setAppId] = useState(() => preferences.getAppId());
111+
const [appId, setAppId] = useState(resolveAppId);
105112
const [consentRequired, setConsentRequiredState] = useState(false);
106113
const [privacyConsentGiven, setPrivacyConsentGivenState] = useState(false);
107114
const [externalUserId, setExternalUserId] = useState<string | undefined>(undefined);
@@ -222,7 +229,7 @@ export function useOneSignal(): UseOneSignalReturn {
222229
};
223230

224231
const load = async () => {
225-
const nextAppId = preferences.getAppId();
232+
const nextAppId = resolveAppId();
226233
const nextConsentRequired = preferences.getConsentRequired();
227234
const nextPrivacyConsentGiven = preferences.getConsentGiven();
228235
const nextIamPaused = preferences.getIamPaused();

examples/demo/src/services/OneSignalApiService.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { userDataFromJson } from '../models/UserData';
55
import type { UserData } from '../models/UserData';
66

77
export const API_KEY = import.meta.env.VITE_ONESIGNAL_API_KEY as string | undefined;
8+
const ANDROID_CHANNEL_ID = import.meta.env.VITE_ONESIGNAL_ANDROID_CHANNEL_ID as string | undefined;
9+
const DEFAULT_ANDROID_CHANNEL_ID = 'b3b015d9-c050-4042-8548-dcc34aa44aa4';
810

911
class OneSignalApiService {
1012
private static instance: OneSignalApiService;
@@ -49,7 +51,7 @@ class OneSignalApiService {
4951
headings = { en: 'Sound Notification' };
5052
contents = { en: 'This notification plays a custom sound' };
5153
extra.ios_sound = 'vine_boom.wav';
52-
extra.android_channel_id = 'b3b015d9-c050-4042-8548-dcc34aa44aa4';
54+
extra.android_channel_id = ANDROID_CHANNEL_ID?.trim() || DEFAULT_ANDROID_CHANNEL_ID;
5355
break;
5456
default:
5557
return false;

examples/demo/src/services/PreferencesService.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
const APP_ID_KEY = 'onesignal.demo.appId';
21
const CONSENT_REQUIRED_KEY = 'onesignal.demo.consentRequired';
32
const CONSENT_GIVEN_KEY = 'onesignal.demo.consentGiven';
43
const EXTERNAL_ID_KEY = 'onesignal.demo.externalUserId';
54
const LOCATION_SHARED_KEY = 'onesignal.demo.locationShared';
65
const IAM_PAUSED_KEY = 'onesignal.demo.iamPaused';
76

8-
const DEFAULT_APP_ID = '77e32082-ea27-42e3-a898-c72e141824ef';
9-
107
function readBoolean(key: string, fallback: boolean): boolean {
118
const value = localStorage.getItem(key);
129
if (value === null) return fallback;
1310
return value === 'true';
1411
}
1512

1613
export default class PreferencesService {
17-
getAppId(): string {
18-
return localStorage.getItem(APP_ID_KEY) ?? DEFAULT_APP_ID;
19-
}
20-
21-
setAppId(value: string): void {
22-
localStorage.setItem(APP_ID_KEY, value);
23-
}
24-
2514
getConsentRequired(): boolean {
2615
return readBoolean(CONSENT_REQUIRED_KEY, false);
2716
}

examples/demo_pods/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
VITE_ONESIGNAL_APP_ID=your_onesignal_app_id
22
VITE_ONESIGNAL_API_KEY=your_rest_api_key
3+
VITE_ONESIGNAL_ANDROID_CHANNEL_ID=
34
VITE_E2E_MODE=false

examples/demo_pods/src/hooks/useOneSignal.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import { NotificationType } from '../models/NotificationType';
1515
import OneSignalApiService, { API_KEY } from '../services/OneSignalApiService';
1616
import PreferencesService from '../services/PreferencesService';
1717

18+
const APP_ID = import.meta.env.VITE_ONESIGNAL_APP_ID as string | undefined;
19+
const DEFAULT_APP_ID = '77e32082-ea27-42e3-a898-c72e141824ef';
20+
21+
function resolveAppId(): string {
22+
return APP_ID?.trim() || DEFAULT_APP_ID;
23+
}
24+
1825
const apiService = OneSignalApiService.getInstance();
1926
const preferences = new PreferencesService();
2027

@@ -101,7 +108,7 @@ export type UseOneSignalReturn = {
101108
};
102109

103110
export function useOneSignal(): UseOneSignalReturn {
104-
const [appId, setAppId] = useState(() => preferences.getAppId());
111+
const [appId, setAppId] = useState(resolveAppId);
105112
const [consentRequired, setConsentRequiredState] = useState(false);
106113
const [privacyConsentGiven, setPrivacyConsentGivenState] = useState(false);
107114
const [externalUserId, setExternalUserId] = useState<string | undefined>(undefined);
@@ -222,7 +229,7 @@ export function useOneSignal(): UseOneSignalReturn {
222229
};
223230

224231
const load = async () => {
225-
const nextAppId = preferences.getAppId();
232+
const nextAppId = resolveAppId();
226233
const nextConsentRequired = preferences.getConsentRequired();
227234
const nextPrivacyConsentGiven = preferences.getConsentGiven();
228235
const nextIamPaused = preferences.getIamPaused();

examples/demo_pods/src/services/OneSignalApiService.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { userDataFromJson } from '../models/UserData';
55
import type { UserData } from '../models/UserData';
66

77
export const API_KEY = import.meta.env.VITE_ONESIGNAL_API_KEY as string | undefined;
8+
const ANDROID_CHANNEL_ID = import.meta.env.VITE_ONESIGNAL_ANDROID_CHANNEL_ID as string | undefined;
9+
const DEFAULT_ANDROID_CHANNEL_ID = 'b3b015d9-c050-4042-8548-dcc34aa44aa4';
810

911
class OneSignalApiService {
1012
private static instance: OneSignalApiService;
@@ -49,7 +51,7 @@ class OneSignalApiService {
4951
headings = { en: 'Sound Notification' };
5052
contents = { en: 'This notification plays a custom sound' };
5153
extra.ios_sound = 'vine_boom.wav';
52-
extra.android_channel_id = 'b3b015d9-c050-4042-8548-dcc34aa44aa4';
54+
extra.android_channel_id = ANDROID_CHANNEL_ID?.trim() || DEFAULT_ANDROID_CHANNEL_ID;
5355
break;
5456
default:
5557
return false;

examples/demo_pods/src/services/PreferencesService.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
const APP_ID_KEY = 'onesignal.demo.appId';
21
const CONSENT_REQUIRED_KEY = 'onesignal.demo.consentRequired';
32
const CONSENT_GIVEN_KEY = 'onesignal.demo.consentGiven';
43
const EXTERNAL_ID_KEY = 'onesignal.demo.externalUserId';
54
const LOCATION_SHARED_KEY = 'onesignal.demo.locationShared';
65
const IAM_PAUSED_KEY = 'onesignal.demo.iamPaused';
76

8-
const DEFAULT_APP_ID = '77e32082-ea27-42e3-a898-c72e141824ef';
9-
107
function readBoolean(key: string, fallback: boolean): boolean {
118
const value = localStorage.getItem(key);
129
if (value === null) return fallback;
1310
return value === 'true';
1411
}
1512

1613
export default class PreferencesService {
17-
getAppId(): string {
18-
return localStorage.getItem(APP_ID_KEY) ?? DEFAULT_APP_ID;
19-
}
20-
21-
setAppId(value: string): void {
22-
localStorage.setItem(APP_ID_KEY, value);
23-
}
24-
2514
getConsentRequired(): boolean {
2615
return readBoolean(CONSENT_REQUIRED_KEY, false);
2716
}

0 commit comments

Comments
 (0)