-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.ts
788 lines (692 loc) · 23.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
const ONESIGNAL_SDK_ID = 'onesignal-sdk';
const ONE_SIGNAL_SCRIPT_SRC = "https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js";
// true if the script is successfully loaded from CDN.
let isOneSignalInitialized = false;
// true if the script fails to load from CDN. A separate flag is necessary
// to disambiguate between a CDN load failure and a delayed call to
// OneSignal#init.
let isOneSignalScriptFailed = false;
if (typeof window !== 'undefined') {
window.OneSignalDeferred = window.OneSignalDeferred || [];
addSDKScript();
}
declare global {
interface Window {
OneSignalDeferred?: OneSignalDeferredLoadedCallback[];
OneSignal?: IOneSignalOneSignal;
safari?: {
pushNotification: any;
};
}
}
/* H E L P E R S */
function handleOnError() {
isOneSignalScriptFailed = true;
}
function addSDKScript() {
const script = document.createElement('script');
script.id = ONESIGNAL_SDK_ID;
script.defer = true;
script.src = ONE_SIGNAL_SCRIPT_SRC;
// Always resolve whether or not the script is successfully initialized.
// This is important for users who may block cdn.onesignal.com w/ adblock.
script.onerror = () => {
handleOnError();
}
document.head.appendChild(script);
}
/**
* The following code is copied directly from the native SDK source file BrowserSupportsPush.ts
* S T A R T
*/
// Checks if the browser supports push notifications by checking if specific
// classes and properties on them exist
function isPushNotificationsSupported() {
return supportsVapidPush() || supportsSafariPush();
}
function isMacOSSafariInIframe(): boolean {
// Fallback detection for Safari on macOS in an iframe context
return window.top !== window && // isContextIframe
navigator.vendor === "Apple Computer, Inc." && // isSafari
navigator.platform === "MacIntel"; // isMacOS
}
function supportsSafariPush(): boolean {
return (window.safari && typeof window.safari.pushNotification !== "undefined") ||
isMacOSSafariInIframe();
}
// Does the browser support the standard Push API
function supportsVapidPush(): boolean {
return typeof PushSubscriptionOptions !== "undefined" &&
PushSubscriptionOptions.prototype.hasOwnProperty("applicationServerKey");
}
/* E N D */
/**
* This is a SPECIAL FUNCTION
* It is a hardcoded implementation copied from the upstream/native WebSDK since we want to return a boolean immediately
* Natively, this is done via the shimloading mechanism (i.e. if the SDK loads, push is supported)
* @PublicApi
*/
const isPushSupported = (): boolean => {
return isPushNotificationsSupported();
}
/**
* @PublicApi
*/
const init = (options: IInitObject): Promise<void> => {
if (isOneSignalInitialized) {
return Promise.reject(`OneSignal is already initialized.`);
}
if (!options || !options.appId) {
throw new Error('You need to provide your OneSignal appId.');
}
if (!document) {
return Promise.reject(`Document is not defined.`);
}
return new Promise<void>((resolve) => {
window.OneSignalDeferred?.push((OneSignal) => {
OneSignal.init(options).then(() => {
isOneSignalInitialized = true;
resolve();
});
});
});
};
interface AutoPromptOptions { force?: boolean; forceSlidedownOverNative?: boolean; slidedownPromptOptions?: IOneSignalAutoPromptOptions; }
interface IOneSignalAutoPromptOptions { force?: boolean; forceSlidedownOverNative?: boolean; isInUpdateMode?: boolean; categoryOptions?: IOneSignalCategories; }
interface IOneSignalCategories { positiveUpdateButton: string; negativeUpdateButton: string; savingButtonText: string; errorButtonText: string; updateMessage: string; tags: IOneSignalTagCategory[]; }
interface IOneSignalTagCategory { tag: string; label: string; checked?: boolean; }
type PushSubscriptionNamespaceProperties = { id: string | null | undefined; token: string | null | undefined; optedIn: boolean; };
type SubscriptionChangeEvent = { previous: PushSubscriptionNamespaceProperties; current: PushSubscriptionNamespaceProperties; };
type NotificationEventName = 'click' | 'foregroundWillDisplay' | 'dismiss' | 'permissionChange' | 'permissionPromptDisplay';
type SlidedownEventName = 'slidedownShown';
type OneSignalDeferredLoadedCallback = (onesignal: IOneSignalOneSignal) => void;
interface IOSNotification {
/**
* The OneSignal notification id;
* - Primary id on OneSignal's REST API and dashboard
*/
readonly notificationId: string;
/**
* Visible title text on the notification
*/
readonly title?: string;
/**
* Visible body text on the notification
*/
readonly body: string;
/**
* Visible icon the notification; URL format
*/
readonly icon?: string;
/**
* Visible small badgeIcon that displays on some devices; URL format
* Example: On Android's status bar
*/
readonly badgeIcon?: string;
/**
* Visible image on the notification; URL format
*/
readonly image?: string;
/**
* Visible buttons on the notification
*/
readonly actionButtons?: IOSNotificationActionButton[];
/**
* If this value is the same as existing notification, it will replace it
* Can be set when creating the notification with "Web Push Topic" on the dashboard
* or web_push_topic from the REST API.
*/
readonly topic?: string;
/**
* Custom object that was sent with the notification;
* definable when creating the notification from the OneSignal REST API or dashboard
*/
readonly additionalData?: object;
/**
* URL to open when clicking or tapping on the notification
*/
readonly launchURL?: string;
/**
* Confirm the push was received by reporting back to OneSignal
*/
readonly confirmDelivery: boolean;
}
interface IOSNotificationActionButton {
/**
* Any unique identifier to represent which button was clicked. This is typically passed back to the service worker
* and host page through events to identify which button was clicked.
* e.g. 'like-button'
*/
readonly actionId: string;
/**
* The notification action button's text.
*/
readonly text: string;
/**
* A valid publicly reachable HTTPS URL to an image.
*/
readonly icon?: string;
/**
* The URL to open the web browser to when this action button is clicked.
*/
readonly launchURL?: string;
}
interface NotificationClickResult {
readonly actionId?: string;
readonly url?: string;
}
type NotificationEventTypeMap = {
'click': NotificationClickEvent;
'foregroundWillDisplay': NotificationForegroundWillDisplayEvent;
'dismiss': NotificationDismissEvent;
'permissionChange': boolean;
'permissionPromptDisplay': void;
}
interface NotificationForegroundWillDisplayEvent {
readonly notification: IOSNotification;
preventDefault(): void;
}
interface NotificationDismissEvent {
notification: IOSNotification;
}
interface NotificationClickEvent {
readonly notification: IOSNotification;
readonly result: NotificationClickResult;
}
interface IInitObject {
appId: string;
subdomainName?: string;
requiresUserPrivacyConsent?: boolean;
promptOptions?: object;
welcomeNotification?: object;
notifyButton?: object;
persistNotification?: boolean;
webhooks?: object;
autoResubscribe?: boolean;
autoRegister?: boolean;
notificationClickHandlerMatch?: string;
notificationClickHandlerAction?: string;
serviceWorkerParam?: { scope: string };
serviceWorkerPath?: string;
serviceWorkerUpdaterPath?: string;
path?: string;
allowLocalhostAsSecureOrigin?: boolean;
[key: string]: any;
}
interface IOneSignalOneSignal {
Slidedown: IOneSignalSlidedown;
Notifications: IOneSignalNotifications;
Session: IOneSignalSession;
User: IOneSignalUser;
Debug: IOneSignalDebug;
login(externalId: string, jwtToken?: string): Promise<void>;
logout(): Promise<void>;
init(options: IInitObject): Promise<void>;
setConsentGiven(consent: boolean): Promise<void>;
setConsentRequired(requiresConsent: boolean): Promise<void>;
}
interface IOneSignalNotifications {
permissionNative: NotificationPermission;
permission: boolean;
setDefaultUrl(url: string): Promise<void>;
setDefaultTitle(title: string): Promise<void>;
isPushSupported(): boolean;
requestPermission(): Promise<void>;
addEventListener<K extends NotificationEventName>(event: K, listener: (obj: NotificationEventTypeMap[K]) => void): void;
removeEventListener<K extends NotificationEventName>(event: K, listener: (obj: NotificationEventTypeMap[K]) => void): void;
}
interface IOneSignalSlidedown {
promptPush(options?: AutoPromptOptions): Promise<void>;
promptPushCategories(options?: AutoPromptOptions): Promise<void>;
promptSms(options?: AutoPromptOptions): Promise<void>;
promptEmail(options?: AutoPromptOptions): Promise<void>;
promptSmsAndEmail(options?: AutoPromptOptions): Promise<void>;
addEventListener(event: SlidedownEventName, listener: (wasShown: boolean) => void): void;
removeEventListener(event: SlidedownEventName, listener: (wasShown: boolean) => void): void;
}
interface IOneSignalDebug {
setLogLevel(logLevel: string): void;
}
interface IOneSignalSession {
sendOutcome(outcomeName: string, outcomeWeight?: number): Promise<void>;
sendUniqueOutcome(outcomeName: string): Promise<void>;
}
interface IOneSignalUser {
PushSubscription: IOneSignalPushSubscription;
addAlias(label: string, id: string): void;
addAliases(aliases: { [key: string]: string }): void;
removeAlias(label: string): void;
removeAliases(labels: string[]): void;
addEmail(email: string): void;
removeEmail(email: string): void;
addSms(smsNumber: string): void;
removeSms(smsNumber: string): void;
addTag(key: string, value: string): void;
addTags(tags: { [key: string]: string }): void;
removeTag(key: string): void;
removeTags(keys: string[]): void;
}
interface IOneSignalPushSubscription {
id: string | null | undefined;
token: string | null | undefined;
optedIn: boolean | undefined;
optIn(): Promise<void>;
optOut(): Promise<void>;
addEventListener(event: 'change', listener: (change: SubscriptionChangeEvent) => void): void;
removeEventListener(event: 'change', listener: (change: SubscriptionChangeEvent) => void): void;
}
function oneSignalLogin(externalId: string, jwtToken?: string): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.login(externalId, jwtToken)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function oneSignalLogout(): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.logout()
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function oneSignalSetConsentGiven(consent: boolean): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.setConsentGiven(consent)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function oneSignalSetConsentRequired(requiresConsent: boolean): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.setConsentRequired(requiresConsent)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function slidedownPromptPush(options?: AutoPromptOptions): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Slidedown.promptPush(options)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function slidedownPromptPushCategories(options?: AutoPromptOptions): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Slidedown.promptPushCategories(options)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function slidedownPromptSms(options?: AutoPromptOptions): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Slidedown.promptSms(options)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function slidedownPromptEmail(options?: AutoPromptOptions): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Slidedown.promptEmail(options)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function slidedownPromptSmsAndEmail(options?: AutoPromptOptions): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Slidedown.promptSmsAndEmail(options)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function slidedownAddEventListener(event: SlidedownEventName, listener: (wasShown: boolean) => void): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Slidedown.addEventListener(event, listener)
});
}
function slidedownRemoveEventListener(event: SlidedownEventName, listener: (wasShown: boolean) => void): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Slidedown.removeEventListener(event, listener)
});
}
function notificationsSetDefaultUrl(url: string): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Notifications.setDefaultUrl(url)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function notificationsSetDefaultTitle(title: string): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Notifications.setDefaultTitle(title)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function notificationsRequestPermission(): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Notifications.requestPermission()
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function notificationsAddEventListener<K extends NotificationEventName>(event: K, listener: (obj: NotificationEventTypeMap[K]) => void): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Notifications.addEventListener(event, listener)
});
}
function notificationsRemoveEventListener<K extends NotificationEventName>(event: K, listener: (obj: NotificationEventTypeMap[K]) => void): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Notifications.removeEventListener(event, listener)
});
}
function sessionSendOutcome(outcomeName: string, outcomeWeight?: number): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Session.sendOutcome(outcomeName, outcomeWeight)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function sessionSendUniqueOutcome(outcomeName: string): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Session.sendUniqueOutcome(outcomeName)
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function userAddAlias(label: string, id: string): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.addAlias(label, id)
});
}
function userAddAliases(aliases: { [key: string]: string }): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.addAliases(aliases)
});
}
function userRemoveAlias(label: string): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.removeAlias(label)
});
}
function userRemoveAliases(labels: string[]): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.removeAliases(labels)
});
}
function userAddEmail(email: string): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.addEmail(email)
});
}
function userRemoveEmail(email: string): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.removeEmail(email)
});
}
function userAddSms(smsNumber: string): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.addSms(smsNumber)
});
}
function userRemoveSms(smsNumber: string): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.removeSms(smsNumber)
});
}
function userAddTag(key: string, value: string): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.addTag(key, value)
});
}
function userAddTags(tags: { [key: string]: string }): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.addTags(tags)
});
}
function userRemoveTag(key: string): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.removeTag(key)
});
}
function userRemoveTags(keys: string[]): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.removeTags(keys)
});
}
function pushSubscriptionOptIn(): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.PushSubscription.optIn()
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function pushSubscriptionOptOut(): Promise<void> {
return new Promise((resolve, reject) => {
if (isOneSignalScriptFailed) {
reject();
}
try {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.PushSubscription.optOut()
.then((value: any) => resolve(value))
.catch((error: any) => reject(error));
});
} catch (error) {
reject(error);
}
});
}
function pushSubscriptionAddEventListener(event: 'change', listener: (change: SubscriptionChangeEvent) => void): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.PushSubscription.addEventListener(event, listener)
});
}
function pushSubscriptionRemoveEventListener(event: 'change', listener: (change: SubscriptionChangeEvent) => void): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.User.PushSubscription.removeEventListener(event, listener)
});
}
function debugSetLogLevel(logLevel: string): void {
window.OneSignalDeferred?.push((OneSignal: IOneSignalOneSignal) => {
OneSignal.Debug.setLogLevel(logLevel)
});
}
const PushSubscriptionNamespace: IOneSignalPushSubscription = {
get id(): string | null | undefined { return window.OneSignal?.User?.PushSubscription?.id },
get token(): string | null | undefined { return window.OneSignal?.User?.PushSubscription?.token },
get optedIn(): boolean | undefined { return window.OneSignal?.User?.PushSubscription?.optedIn },
optIn: pushSubscriptionOptIn,
optOut: pushSubscriptionOptOut,
addEventListener: pushSubscriptionAddEventListener,
removeEventListener: pushSubscriptionRemoveEventListener,
};
const UserNamespace: IOneSignalUser = {
addAlias: userAddAlias,
addAliases: userAddAliases,
removeAlias: userRemoveAlias,
removeAliases: userRemoveAliases,
addEmail: userAddEmail,
removeEmail: userRemoveEmail,
addSms: userAddSms,
removeSms: userRemoveSms,
addTag: userAddTag,
addTags: userAddTags,
removeTag: userRemoveTag,
removeTags: userRemoveTags,
PushSubscription: PushSubscriptionNamespace,
};
const SessionNamespace: IOneSignalSession = {
sendOutcome: sessionSendOutcome,
sendUniqueOutcome: sessionSendUniqueOutcome,
};
const DebugNamespace: IOneSignalDebug = {
setLogLevel: debugSetLogLevel,
};
const SlidedownNamespace: IOneSignalSlidedown = {
promptPush: slidedownPromptPush,
promptPushCategories: slidedownPromptPushCategories,
promptSms: slidedownPromptSms,
promptEmail: slidedownPromptEmail,
promptSmsAndEmail: slidedownPromptSmsAndEmail,
addEventListener: slidedownAddEventListener,
removeEventListener: slidedownRemoveEventListener,
};
const NotificationsNamespace: IOneSignalNotifications = {
get permissionNative(): NotificationPermission { return window.OneSignal?.Notifications?.permissionNative ?? 'default'; },
get permission(): boolean { return window.OneSignal?.Notifications?.permission ?? false; },
setDefaultUrl: notificationsSetDefaultUrl,
setDefaultTitle: notificationsSetDefaultTitle,
isPushSupported,
requestPermission: notificationsRequestPermission,
addEventListener: notificationsAddEventListener,
removeEventListener: notificationsRemoveEventListener,
};
const OneSignalNamespace: IOneSignalOneSignal = {
login: oneSignalLogin,
logout: oneSignalLogout,
init,
setConsentGiven: oneSignalSetConsentGiven,
setConsentRequired: oneSignalSetConsentRequired,
Slidedown: SlidedownNamespace,
Notifications: NotificationsNamespace,
Session: SessionNamespace,
User: UserNamespace,
Debug: DebugNamespace,
};
const OneSignal = OneSignalNamespace;
export default OneSignal;