-
Notifications
You must be signed in to change notification settings - Fork 115
/
OneSignal.ts
executable file
·1055 lines (958 loc) · 41 KB
/
OneSignal.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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import bowser from 'bowser';
import Environment from './Environment';
import { InvalidArgumentError, InvalidArgumentReason } from './errors/InvalidArgumentError';
import { InvalidStateError, InvalidStateReason } from './errors/InvalidStateError';
import { NotSubscribedError, NotSubscribedReason } from './errors/NotSubscribedError';
import { SdkInitError, SdkInitErrorKind } from './errors/SdkInitError';
import Event from './Event';
import EventHelper from './helpers/EventHelper';
import HttpHelper from './helpers/HttpHelper';
import InitHelper, { RegisterOptions } from './helpers/InitHelper';
import MainHelper from './helpers/MainHelper';
import SubscriptionHelper from './helpers/SubscriptionHelper';
import TestHelper from './helpers/TestHelper';
import LimitStore from './LimitStore';
import AltOriginManager from './managers/AltOriginManager';
import LegacyManager from './managers/LegacyManager';
import SdkEnvironment from './managers/SdkEnvironment';
import { AppConfig, AppUserConfig, AppUserConfigNotifyButton } from './models/AppConfig';
import Context from './models/Context';
import { Notification, NotificationReceived, NotificationClicked } from "./models/Notification";
import { NotificationActionButton } from './models/NotificationActionButton';
import { NotificationPermission } from './models/NotificationPermission';
import { WindowEnvironmentKind } from './models/WindowEnvironmentKind';
import ProxyFrame from './modules/frames/ProxyFrame';
import ProxyFrameHost from './modules/frames/ProxyFrameHost';
import SubscriptionModal from './modules/frames/SubscriptionModal';
import SubscriptionModalHost from './modules/frames/SubscriptionModalHost';
import SubscriptionPopup from './modules/frames/SubscriptionPopup';
import SubscriptionPopupHost from './modules/frames/SubscriptionPopupHost';
import OneSignalApi from './OneSignalApi';
import Popover from './popover/Popover';
import Database from './services/Database';
import Utils from "./utils/Utils";
import IndexedDb from './services/IndexedDb';
import {
awaitOneSignalInitAndSupported,
awaitSdkEvent,
executeCallback,
getConsoleStyle,
isValidEmail,
logMethodCall,
} from './utils';
import { ValidatorUtils } from './utils/ValidatorUtils';
import { DeviceRecord } from './models/DeviceRecord';
import TimedLocalStorage from './modules/TimedLocalStorage';
import { EmailProfile } from './models/EmailProfile';
import { EmailDeviceRecord } from './models/EmailDeviceRecord';
import Emitter, { EventHandler } from './libraries/Emitter';
import Log from './libraries/Log';
import ConfigManager from "./managers/ConfigManager";
import OneSignalUtils from "./utils/OneSignalUtils";
import { ProcessOneSignalPushCalls } from "./utils/ProcessOneSignalPushCalls";
import { AutoPromptOptions } from "./managers/PromptsManager";
export default class OneSignal {
/**
* Pass in the full URL of the default page you want to open when a notification is clicked.
* @PublicApi
*/
static async setDefaultNotificationUrl(url: string) {
if (!ValidatorUtils.isValidUrl(url, { allowNull: true }))
throw new InvalidArgumentError('url', InvalidArgumentReason.Malformed);
await awaitOneSignalInitAndSupported();
logMethodCall('setDefaultNotificationUrl', url);
const appState = await Database.getAppState();
appState.defaultNotificationUrl = url;
await Database.setAppState(appState);
}
/**
* Sets the default title to display on notifications. Will default to the page's document.title if you don't call this.
* @remarks Either DB value defaultTitle or pageTitle is used when showing a notification title.
* @PublicApi
*/
static async setDefaultTitle(title: string) {
await awaitOneSignalInitAndSupported();
logMethodCall('setDefaultTitle', title);
const appState = await Database.getAppState();
appState.defaultNotificationTitle = title;
await Database.setAppState(appState);
}
/**
* @PublicApi
*/
static async setEmail(email: string, options?: SetEmailOptions): Promise<string> {
if (!email)
throw new InvalidArgumentError('email', InvalidArgumentReason.Empty);
if (!isValidEmail(email))
throw new InvalidArgumentError('email', InvalidArgumentReason.Malformed);
// emailAuthHash is expected to be a 64 character SHA-256 hex hash
if (options && options.emailAuthHash && options.emailAuthHash.length !== 64) {
throw new InvalidArgumentError('options.emailAuthHash', InvalidArgumentReason.Malformed);
}
await awaitOneSignalInitAndSupported();
logMethodCall('setEmail', email, options);
const appConfig = await Database.getAppConfig();
const { deviceId } = await Database.getSubscription();
const existingEmailProfile = await Database.getEmailProfile();
if (appConfig.emailAuthRequired && !(options && options.emailAuthHash)) {
throw new InvalidArgumentError('options.emailAuthHash', InvalidArgumentReason.Empty);
}
const newEmailProfile = new EmailProfile(existingEmailProfile.emailId, email);
if (options && options.emailAuthHash) {
newEmailProfile.emailAuthHash = options.emailAuthHash
}
const isExistingEmailSaved = !!existingEmailProfile.emailId;
if (isExistingEmailSaved && appConfig.emailAuthRequired) {
// If we already have a saved email player ID, make a PUT call to update the existing email record
newEmailProfile.emailId = await OneSignalApi.updateEmailRecord(
appConfig,
newEmailProfile,
deviceId
);
} else {
// Otherwise, make a POST call to create a new email record
newEmailProfile.emailId = await OneSignalApi.createEmailRecord(
appConfig,
newEmailProfile,
deviceId
);
}
const isExistingPushRecordSaved = deviceId;
if (
/* If we are subscribed to web push */
isExistingPushRecordSaved &&
(
/* And if we previously saved an email ID and it's different from the new returned ID */
(
!isExistingEmailSaved ||
existingEmailProfile.emailId !== newEmailProfile.emailId
) ||
/* Or if we previously saved an email and the email changed */
(
!existingEmailProfile.emailAddress ||
newEmailProfile.emailAddress !== existingEmailProfile.emailAddress
)
)
) {
// Then update the push device record with a reference to the new email ID and email address
await OneSignalApi.updatePlayer(
appConfig.appId,
deviceId,
{
parent_player_id: newEmailProfile.emailId,
email: newEmailProfile.emailAddress
}
);
}
await Database.setEmailProfile(newEmailProfile);
return newEmailProfile.emailId;
}
/**
* @PublicApi
*/
static async logoutEmail() {
await awaitOneSignalInitAndSupported();
const appConfig = await Database.getAppConfig();
const emailProfile = await Database.getEmailProfile();
const { deviceId } = await Database.getSubscription();
if (!emailProfile.emailId) {
Log.warn(new NotSubscribedError(NotSubscribedReason.NoEmailSet));
return;
}
if (!deviceId) {
Log.warn(new NotSubscribedError(NotSubscribedReason.NoDeviceId));
return;
}
if (!await OneSignalApi.logoutEmail(appConfig, emailProfile, deviceId)) {
Log.warn("Failed to logout email.");
return;
}
await Database.setEmailProfile(new EmailProfile());
}
/**
* Returns true if the current browser supports web push.
* @PublicApi
*/
static isPushNotificationsSupported(): boolean {
logMethodCall('isPushNotificationsSupported');
/*
Push notification support is checked in the initial entry code. If in an unsupported environment, a stubbed empty
version of the SDK will be loaded instead. This file will only be loaded if push notifications are supported.
*/
return true;
}
static async initializeConfig(options: AppUserConfig) {
const appConfig = await new ConfigManager().getAppConfig(options);
Log.debug(`OneSignal: Final web app config: %c${JSON.stringify(appConfig, null, 4)}`, getConsoleStyle('code'));
OneSignal.context = new Context(appConfig);
OneSignal.config = OneSignal.context.appConfig;
}
/**
* Initializes the SDK, called by the developer.
* @PublicApi
*/
static async init(options: AppUserConfig) {
logMethodCall('init');
await InitHelper.polyfillSafariFetch();
InitHelper.errorIfInitAlreadyCalled();
await OneSignal.initializeConfig(options);
if (!OneSignal.config) {
throw new Error("OneSignal config not initialized!");
}
if (bowser.safari && !OneSignal.config.safariWebId) {
/**
* Don't throw an error for missing Safari config; many users set up
* support on Chrome/Firefox and don't intend to support Safari but don't
* place conditional initialization checks.
*/
Log.warn(new SdkInitError(SdkInitErrorKind.MissingSafariWebId));
return;
}
if (OneSignal.config.userConfig.requiresUserPrivacyConsent) {
const providedConsent = await Database.getProvideUserConsent();
if (!providedConsent) {
OneSignal.pendingInit = true;
return;
}
}
await OneSignal.delayedInit();
}
private static async delayedInit(): Promise<void> {
OneSignal.pendingInit = false;
// Ignore Promise as doesn't return until the service worker becomes active.
OneSignal.context.workerMessenger.listen();
async function __init() {
if (OneSignal.__initAlreadyCalled)
return;
else
OneSignal.__initAlreadyCalled = true;
MainHelper.fixWordpressManifestIfMisplaced();
OneSignal.emitter.on(OneSignal.EVENTS.NATIVE_PROMPT_PERMISSIONCHANGED, EventHelper.onNotificationPermissionChange);
OneSignal.emitter.on(OneSignal.EVENTS.SUBSCRIPTION_CHANGED, EventHelper._onSubscriptionChanged);
OneSignal.emitter.on(OneSignal.EVENTS.SDK_INITIALIZED, InitHelper.onSdkInitialized);
if (OneSignalUtils.isUsingSubscriptionWorkaround()) {
/**
* The user may have forgot to choose a subdomain in his web app setup.
*
* Or, the user may have an HTTP & HTTPS site while using an HTTPS-only
* config on both variants. This would cause the HTTPS site to work
* perfectly, while causing errors and preventing web push from working
* on the HTTP site.
*/
if (!OneSignal.config || !OneSignal.config.subdomain)
throw new SdkInitError(SdkInitErrorKind.MissingSubdomain);
/**
* The iFrame may never load (e.g. OneSignal might be down), in which
* case the rest of the SDK's initialization will be blocked. This is a
* good thing! We don't want to access IndexedDb before we know which
* origin to store data on.
*/
OneSignal.proxyFrameHost = await AltOriginManager.discoverAltOrigin(OneSignal.config);
}
window.addEventListener('focus', () => {
// Checks if permission changed every time a user focuses on the page,
// since a user has to click out of and back on the page to check permissions
MainHelper.checkAndTriggerNotificationPermissionChanged();
});
await InitHelper.initSaveState(document.title);
await InitHelper.saveInitOptions();
if (SdkEnvironment.getWindowEnv() === WindowEnvironmentKind.CustomIframe)
await Event.trigger(OneSignal.EVENTS.SDK_INITIALIZED);
else
await InitHelper.internalInit();
}
if (document.readyState === 'complete' || document.readyState === 'interactive')
await __init();
else {
Log.debug('OneSignal: Waiting for DOMContentLoaded or readyStateChange event before continuing' +
' initialization...');
window.addEventListener('DOMContentLoaded', () => { __init(); });
document.onreadystatechange = () => {
if (document.readyState === 'complete' || document.readyState === 'interactive')
__init();
};
}
}
/**
* Call after use accepts your user consent agreement
* @PublicApi
*/
public static async provideUserConsent(consent: boolean): Promise<void> {
await Database.setProvideUserConsent(consent);
if (consent && OneSignal.pendingInit)
await OneSignal.delayedInit();
}
/**
* Prompts the user to subscribe using the remote local notification workaround for HTTP sites.
* @PublicApi
*/
public static async showHttpPermissionRequest(options?: AutoPromptOptions): Promise<any> {
Log.debug('Called showHttpPermissionRequest(), redirecting to HTTP prompt.');
OneSignal.showHttpPrompt(options).catch(e => Log.info(e));
}
/**
* Shows a sliding modal prompt on the page for users to trigger the HTTP popup window to subscribe.
* @PublicApi
*/
public static async showHttpPrompt(options?: AutoPromptOptions) {
await awaitOneSignalInitAndSupported();
await OneSignal.context.promptsManager.internalShowSlidedownPrompt(options);
}
/**
* Shows a native browser prompt.
* @PublicApi
*/
public static async showNativePrompt(): Promise<void> {
await awaitOneSignalInitAndSupported();
await OneSignal.context.promptsManager.internalShowNativePrompt();
}
/**
* Shows a sliding modal prompt on the page for users.
* @PublicApi
*/
public static async showSlidedownPrompt(options?: AutoPromptOptions): Promise<void> {
await awaitOneSignalInitAndSupported();
await OneSignal.context.promptsManager.internalShowSlidedownPrompt(options);
}
/**
* Prompts the user to subscribe.
* @PublicApi
*/
static async registerForPushNotifications(options?: RegisterOptions): Promise<void> {
if (!OneSignal.initialized) {
await new Promise((resolve, _reject) => {
OneSignal.emitter.once(OneSignal.EVENTS.SDK_INITIALIZED, async () => {
await InitHelper.registerForPushNotifications(options);
return resolve();
});
})
} else
return await InitHelper.registerForPushNotifications(options);
}
/**
* Returns a promise that resolves to the browser's current notification permission as
* 'default', 'granted', or 'denied'.
* @param callback A callback function that will be called when the browser's current notification permission
* has been obtained, with one of 'default', 'granted', or 'denied'.
* @PublicApi
*/
public static async getNotificationPermission(onComplete?: Function): Promise<NotificationPermission> {
await awaitOneSignalInitAndSupported();
return OneSignal.privateGetNotificationPermission(onComplete);
}
static async privateGetNotificationPermission(onComplete?: Function): Promise<NotificationPermission> {
const permission = await OneSignal.context.permissionManager.getNotificationPermission(
OneSignal.config!.safariWebId
);
if (onComplete)
onComplete(permission);
return permission;
}
/**
* @PublicApi
*/
static async getTags(callback?: Action<any>) {
await awaitOneSignalInitAndSupported();
logMethodCall('getTags', callback);
const { appId } = await Database.getAppConfig();
const { deviceId } = await Database.getSubscription();
if (!deviceId) {
// TODO: Throw an error here in future v2; for now it may break existing client implementations.
Log.info(new NotSubscribedError(NotSubscribedReason.NoDeviceId));
return null;
}
const { tags } = await OneSignalApi.getPlayer(appId, deviceId);
executeCallback(callback, tags);
return tags;
}
/**
* @PublicApi
*/
static async sendTag(key: string, value: any, callback?: Action<Object>): Promise<Object | null> {
const tag = {} as {[key: string]: any};
tag[key] = value;
return await OneSignal.sendTags(tag, callback);
}
/**
* @PublicApi
*/
static async sendTags(tags: {[key: string]: any}, callback?: Action<Object>): Promise<Object | null> {
await awaitOneSignalInitAndSupported();
logMethodCall('sendTags', tags, callback);
if (!tags || Object.keys(tags).length === 0) {
// TODO: Throw an error here in future v2; for now it may break existing client implementations.
Log.info(new InvalidArgumentError('tags', InvalidArgumentReason.Empty));
return null;
}
// Our backend considers false as removing a tag, so convert false -> "false" to allow storing as a value
Object.keys(tags).forEach(key => {
if (tags[key] === false)
tags[key] = "false";
});
const { appId } = await Database.getAppConfig();
const emailProfile = await Database.getEmailProfile();
if (emailProfile.emailId) {
await OneSignalApi.updatePlayer(appId, emailProfile.emailId, {
tags: tags,
email_auth_hash: emailProfile.emailAuthHash,
});
}
var { deviceId } = await Database.getSubscription();
if (!deviceId) {
await awaitSdkEvent(OneSignal.EVENTS.REGISTERED);
}
// After the user subscribers, he will have a device ID, so get it again
var { deviceId: newDeviceId } = await Database.getSubscription();
await OneSignalApi.updatePlayer(appId, newDeviceId!, {
tags: tags
});
executeCallback(callback, tags);
return tags;
}
/**
* @PublicApi
*/
static async deleteTag(tag: string): Promise<Array<string>> {
return await OneSignal.deleteTags([tag]);
}
/**
* @PublicApi
*/
static async deleteTags(tags: Array<string>, callback?: Action<Array<string>>): Promise<Array<string>> {
await awaitOneSignalInitAndSupported();
logMethodCall('deleteTags', tags, callback);
if (!ValidatorUtils.isValidArray(tags))
throw new InvalidArgumentError('tags', InvalidArgumentReason.Malformed);
if (tags.length === 0) {
// TODO: Throw an error here in future v2; for now it may break existing client implementations.
Log.info(new InvalidArgumentError('tags', InvalidArgumentReason.Empty));
}
const tagsToSend = {} as {[key: string]: string};
for (let tag of tags) {
tagsToSend[tag] = '';
}
const deletedTags = await OneSignal.sendTags(tagsToSend);
if (deletedTags) {
const deletedTagKeys = Object.keys(deletedTags);
executeCallback(callback, deletedTagKeys);
return deletedTagKeys;
}
return [];
}
/**
* @PublicApi
*/
public static async setExternalUserId(externalUserId: string | undefined | null): Promise<void> {
await awaitOneSignalInitAndSupported();
logMethodCall("setExternalUserId");
const isExistingUser = await this.context.subscriptionManager.isAlreadyRegisteredWithOneSignal();
if (!isExistingUser) {
await awaitSdkEvent(OneSignal.EVENTS.REGISTERED);
}
await Promise.all([
OneSignal.database.setExternalUserId(externalUserId),
OneSignal.context.updateManager.sendExternalUserIdUpdate(externalUserId),
]);
}
/**
* @PublicApi
*/
public static async getExternalUserId(): Promise<string | undefined | null> {
await awaitOneSignalInitAndSupported();
logMethodCall("getExternalUserId");
return await OneSignal.database.getExternalUserId();
}
/**
* @PublicApi
*/
public static async removeExternalUserId(): Promise<void> {
await awaitOneSignalInitAndSupported();
logMethodCall("removeExternalUserId");
const isExistingUser = await this.context.subscriptionManager.isAlreadyRegisteredWithOneSignal();
if (!isExistingUser) {
Log.warn("User is not subscribed, cannot remove external user id.");
return;
}
await Promise.all([
OneSignal.database.setExternalUserId(undefined),
OneSignal.context.updateManager.sendExternalUserIdUpdate(undefined),
]);
}
/**
* @PublicApi
*/
static async addListenerForNotificationOpened(callback?: Action<Notification>) {
await awaitOneSignalInitAndSupported();
logMethodCall('addListenerForNotificationOpened', callback);
OneSignal.emitter.once(OneSignal.EVENTS.NOTIFICATION_CLICKED, notification => {
executeCallback(callback, notification);
});
if (OneSignal.config) {
EventHelper.fireStoredNotificationClicks(OneSignal.config.pageUrl || OneSignal.config.userConfig.pageUrl);
}
}
/**
* @PublicApi
* @Deprecated
*/
static async getIdsAvailable(
callback?: Action<{userId: string | undefined | null, registrationId: string | undefined | null}>):
Promise<{userId: string | undefined | null, registrationId: string | undefined | null}> {
await awaitOneSignalInitAndSupported();
logMethodCall('getIdsAvailable', callback);
const { deviceId, subscriptionToken } = await Database.getSubscription();
const bundle = {
userId: deviceId,
registrationId: subscriptionToken
};
executeCallback(callback, bundle);
return bundle;
}
/**
* Returns a promise that resolves to true if all required conditions for push messaging are met; otherwise resolves to false.
* @param callback A callback function that will be called when the current subscription status has been obtained.
* @PublicApi
*/
static async isPushNotificationsEnabled(callback?: Action<boolean>): Promise<boolean> {
await awaitOneSignalInitAndSupported();
return OneSignal.privateIsPushNotificationsEnabled(callback);
}
static async privateIsPushNotificationsEnabled(callback?: Action<boolean>): Promise<boolean> {
logMethodCall('isPushNotificationsEnabled', callback);
const context: Context = OneSignal.context;
const subscriptionState = await context.subscriptionManager.getSubscriptionState();
executeCallback(callback, subscriptionState.subscribed && !subscriptionState.optedOut);
return subscriptionState.subscribed && !subscriptionState.optedOut;
}
/**
* @PublicApi
*/
static async setSubscription(newSubscription: boolean): Promise<void> {
await awaitOneSignalInitAndSupported();
logMethodCall('setSubscription', newSubscription);
const appConfig = await Database.getAppConfig();
const { appId } = appConfig;
const subscription = await Database.getSubscription();
const { deviceId } = subscription;
if (!appConfig.appId)
throw new InvalidStateError(InvalidStateReason.MissingAppId);
if (!ValidatorUtils.isValidBoolean(newSubscription))
throw new InvalidArgumentError('newSubscription', InvalidArgumentReason.Malformed);
if (!deviceId) {
// TODO: Throw an error here in future v2; for now it may break existing client implementations.
Log.info(new NotSubscribedError(NotSubscribedReason.NoDeviceId));
return;
}
subscription.optedOut = !newSubscription;
await OneSignalApi.updatePlayer(appId, deviceId, {
notification_types: MainHelper.getNotificationTypeFromOptIn(newSubscription)
});
await Database.setSubscription(subscription);
EventHelper.onInternalSubscriptionSet(subscription.optedOut);
EventHelper.checkAndTriggerSubscriptionChanged();
}
/**
* @PendingPublicApi
*/
static async isOptedOut(callback?: Action<boolean | undefined | null>):
Promise<boolean | undefined | null> {
await awaitOneSignalInitAndSupported();
return OneSignal.internalIsOptedOut(callback);
}
static async internalIsOptedOut(callback?: Action<boolean | undefined | null>):
Promise<boolean | undefined | null> {
logMethodCall('isOptedOut', callback);
const { optedOut } = await Database.getSubscription();
executeCallback(callback, optedOut);
return optedOut;
}
/**
* Returns a promise that resolves once the manual subscription override has been set.
* @private
* @PendingPublicApi
*/
static async optOut(doOptOut: boolean, callback?: Action<void>): Promise<void> {
await awaitOneSignalInitAndSupported();
logMethodCall('optOut', doOptOut, callback);
if (!ValidatorUtils.isValidBoolean(doOptOut))
throw new InvalidArgumentError('doOptOut', InvalidArgumentReason.Malformed);
await OneSignal.setSubscription(!doOptOut);
executeCallback(callback);
}
/**
* Returns a promise that resolves to the stored OneSignal email ID if one is set; otherwise null.
* @param callback A function accepting one parameter for the OneSignal email ID.
* @PublicApi
*/
static async getEmailId(callback?: Action<string | undefined>): Promise<string | undefined> {
await awaitOneSignalInitAndSupported();
logMethodCall('getEmailId', callback);
const emailProfile = await Database.getEmailProfile();
const emailId = emailProfile.emailId;
executeCallback(callback, emailId);
return emailId;
}
/**
* Returns a promise that resolves to the stored OneSignal user ID if one is set; otherwise null.
* @param callback A function accepting one parameter for the OneSignal user ID.
* @PublicApi
*/
static async getUserId(callback?: Action<string | undefined | null>): Promise<string | undefined | null> {
await awaitOneSignalInitAndSupported();
logMethodCall('getUserId', callback);
const subscription = await Database.getSubscription();
const deviceId = subscription.deviceId;
executeCallback(callback, deviceId);
return deviceId;
}
/**
* Returns a promise that resolves to the stored push token if one is set; otherwise null.
* @PublicApi
*/
static async getRegistrationId(callback?: Action<string | undefined | null>):
Promise<string | undefined | null> {
await awaitOneSignalInitAndSupported();
logMethodCall('getRegistrationId', callback);
const subscription = await Database.getSubscription();
const subscriptionToken = subscription.subscriptionToken;
executeCallback(callback, subscriptionToken);
return subscriptionToken;
}
/**
* Returns a promise that resolves to false if setSubscription(false) is "in effect". Otherwise returns true.
* This means a return value of true does not mean the user is subscribed, only that the user did not call
* setSubcription(false).
* @private
* @PublicApi (given to customers)
*/
static async getSubscription(callback?: Action<boolean>): Promise<boolean> {
await awaitOneSignalInitAndSupported();
return await OneSignal.privateGetSubscription(callback);
}
static async privateGetSubscription(callback?: Action<boolean>): Promise<boolean> {
logMethodCall('getSubscription', callback);
const subscription = await Database.getSubscription();
const subscriptionStatus = !subscription.optedOut;
executeCallback(callback, subscriptionStatus);
return subscriptionStatus;
}
/**
* @PublicApi
*/
static async sendSelfNotification(title: string = 'OneSignal Test Message',
message: string = 'This is an example notification.',
url: string = new URL(location.href).origin + '?_osp=do_not_open',
icon: URL,
data: Map<String, any>,
buttons: Array<NotificationActionButton>): Promise<void> {
await awaitOneSignalInitAndSupported();
logMethodCall('sendSelfNotification', title, message, url, icon, data, buttons);
const appConfig = await Database.getAppConfig();
const subscription = await Database.getSubscription();
if (!appConfig.appId)
throw new InvalidStateError(InvalidStateReason.MissingAppId);
if (!(await OneSignal.isPushNotificationsEnabled()))
throw new NotSubscribedError(NotSubscribedReason.NoDeviceId);
if (!ValidatorUtils.isValidUrl(url))
throw new InvalidArgumentError('url', InvalidArgumentReason.Malformed);
if (!ValidatorUtils.isValidUrl(icon, { allowEmpty: true, requireHttps: true }))
throw new InvalidArgumentError('icon', InvalidArgumentReason.Malformed);
if (subscription.deviceId) {
await OneSignalApi.sendNotification(appConfig.appId, [subscription.deviceId], {'en': title}, {'en': message},
url, icon, data, buttons);
}
}
/**
* Used to load OneSignal asynchronously from a webpage
* Allows asynchronous function queuing while the SDK loads in the browser with <script src="..." async/>
* @PublicApi
* @param item - Ether a function or an arry with a OneSignal function name followed by it's parameters
* @Example
* OneSignal.push(["functionName", param1, param2]);
* OneSignal.push(function() { OneSignal.functionName(param1, param2); });
*/
static push(item: Function | object[]) {
ProcessOneSignalPushCalls.processItem(OneSignal, item);
}
/**
* Used to subscribe to OneSignal events such as "subscriptionChange"
* Fires each time the event occurs
* @param event - Event name to subscribe to
* @param listener - Listener to fire when event happens
* @PublicApi
*/
static on(event: string, listener: EventHandler): Emitter {
return this.emitter.on(event, listener);
}
/**
* Used to un-subscribe from OneSignal events such as "subscriptionChange"
* @param event - Event name to un-subscribe from
* @param listener - Event listener to remove from the collection for a specified event
* @PublicApi
*/
static off(event: string, listener: EventHandler): Emitter {
return this.emitter.off(event, listener);
}
/**
* Used to subscribe to OneSignal events such as "subscriptionChange"
* Fires only once
* @param event - Event name to subscribe to
* @param listener - Listener to fire when event happens
* @PublicApi
*/
static once(event: string, listener: EventHandler): Emitter {
return this.emitter.once(event, listener);
}
public static async sendOutcome(outcomeName: string, outcomeWeight?: number | undefined): Promise<void> {
if (!outcomeName) {
Log.error("Outcome name is required");
return;
}
if (typeof outcomeWeight !== "undefined" && typeof outcomeWeight !== "number") {
Log.error("Outcome weight can only be a number if present.");
return;
}
// TODO: check built-in outcome names? not allow sending?
await awaitOneSignalInitAndSupported();
const isSubscribed = await OneSignal.privateIsPushNotificationsEnabled();
if (!isSubscribed) {
Log.warn("Reporting outcomes is supported only for subscribed users.");
return;
}
/**
* Flow:
* 1. check if the url was opened as a result of a notif;
* 2. if so, send an api call reporting direct notification outcome
* (currently takes into account the match strategy selected in the app's settings);
* 3. else check all received notifs within timeframe from config;
* 4. send an api call reporting an influenced outcome for each matching notification
* respecting the limit from config too;
* 5. if no influencing notification found, report unattributed outcome to the api.
*/
// TODO save all intended calls into IDB
// TODO perform all calls removing the record about them from IDB after finished successfully
/* direct notification */
if (OneSignal.config!.userConfig.outcomes!.direct.enabled) {
const clickedNotification: NotificationClicked | null = await OneSignal.database.getNotificationClickedByUrl(
window.location.href, OneSignal.config!.appId);
if (clickedNotification) {
await OneSignal.context.updateManager.sendOutcomeDirect(
clickedNotification.appId, clickedNotification.notificationId, outcomeName, outcomeWeight);
return;
}
}
/* influencing notifications */
if (OneSignal.config!.userConfig.outcomes!.indirect.enabled) {
const timeframeMs = OneSignal.config!.userConfig.outcomes!.indirect.influencedTimePeriodMin * 60 * 1000;
const beginningOfTimeframe = new Date(new Date().getTime() - timeframeMs);
const maxTimestamp = beginningOfTimeframe.getTime().toString();
const matchingNotifications: NotificationReceived[] =
await OneSignal.database.getNotificationReceivedForTimeRange(maxTimestamp);
if (matchingNotifications.length > 0) {
const max: number = OneSignal.config!.userConfig.outcomes!.indirect.influencedNotificationsLimit;
/**
* To handle correctly the case when user got subscribed to a new app id
* we check the appId on notifications to match the current app.
*/
Utils.sortArrayOfObjects(matchingNotifications, (notif: NotificationReceived) => notif.timestamp, true, true);
const notificationIds = matchingNotifications.filter(notif => notif.appId === OneSignal.config!.appId)
.slice(0, max).map(notif => notif.notificationId);
await OneSignal.context.updateManager.sendOutcomeInfluenced(
OneSignal.config!.appId, notificationIds, outcomeName, outcomeWeight);
return;
}
}
/* unattributed outcome report */
if (OneSignal.config!.userConfig.outcomes!.unattributed.enabled) {
await OneSignal.context.updateManager.sendOutcomeUnattributed(
OneSignal.config!.appId, outcomeName, outcomeWeight);
}
}
static __doNotShowWelcomeNotification: boolean;
static VERSION = Environment.version();
static _VERSION = Environment.version();
static sdkEnvironment = SdkEnvironment;
static _notificationOpenedCallbacks = [];
static _idsAvailable_callback = [];
static _defaultLaunchURL = null;
static config: AppConfig | null = null;
static _sessionInitAlreadyRunning = false;
static _isNotificationEnabledCallback = [];
static _subscriptionSet = true;
static modalUrl = null;
static _windowWidth = 650;
static _windowHeight = 568;
static _isNewVisitor = false;
static _channel = null;
static timedLocalStorage = TimedLocalStorage;
static initialized = false;
static notifyButton: AppUserConfigNotifyButton | null = null;
static store = LimitStore;
static environment = Environment;
static database = Database;
static event = Event;
static browser = bowser;
static popover: Popover | null = null;
static log = Log;
static api = OneSignalApi;
static indexedDb = IndexedDb;
static mainHelper = MainHelper;
static subscriptionHelper = SubscriptionHelper;
static httpHelper = HttpHelper;
static eventHelper = EventHelper;
static initHelper = InitHelper;
static testHelper = TestHelper;
private static pendingInit: boolean = true;
static subscriptionPopup: SubscriptionPopup;
static subscriptionPopupHost: SubscriptionPopupHost;
static subscriptionModal: SubscriptionModal;
static subscriptionModalHost: SubscriptionModalHost;
static proxyFrameHost: ProxyFrameHost;
static proxyFrame: ProxyFrame;
static emitter: Emitter = new Emitter();
/**
* The additional path to the worker file.
*
* Usually just the filename (in case the file is named differently), but also supports cases where the folder
* is different.
*
* However, the init options 'path' should be used to specify the folder path instead since service workers will not
* auto-update correctly on HTTPS site load if the config init options 'path' is not set.
*/
static SERVICE_WORKER_UPDATER_PATH = 'OneSignalSDKUpdaterWorker.js';
static SERVICE_WORKER_PATH = 'OneSignalSDKWorker.js';
/**
* By default, the service worker is expected to be accessible at the root scope. If the service worker is only
* available with in a sub-directory, SERVICE_WORKER_PARAM must be changed to the sub-directory (with a trailing
* slash). This would allow pages to function correctly as not to block the service worker ready call, which would
* hang indefinitely if we requested root scope registration but the service was only available in a child scope.
*/
static SERVICE_WORKER_PARAM: { scope: string } = {scope: '/'};
static _LOGGING = false;
static LOGGING = false;
static _usingNativePermissionHook = false;
static _initCalled = false;
static __initAlreadyCalled = false;
static context: Context;
static checkAndWipeUserSubscription = function () { }
static DeviceRecord = DeviceRecord;
static EmailDeviceRecord = EmailDeviceRecord;
static notificationPermission = NotificationPermission;
/**
* Used by Rails-side HTTP popup. Must keep the same name.
* @InternalApi
*/
static _initHttp = HttpHelper.initHttp;
/**
* Used by Rails-side HTTP popup. Must keep the same name.
* @InternalApi
*/
static _initPopup = () => OneSignal.subscriptionPopup.subscribe();
static POSTMAM_COMMANDS = {
CONNECTED: 'connect',
REMOTE_NOTIFICATION_PERMISSION: 'postmam.remoteNotificationPermission',
REMOTE_DATABASE_GET: 'postmam.remoteDatabaseGet',
REMOTE_DATABASE_PUT: 'postmam.remoteDatabasePut',
REMOTE_DATABASE_REMOVE: 'postmam.remoteDatabaseRemove',
REMOTE_OPERATION_COMPLETE: 'postman.operationComplete',
REMOTE_RETRIGGER_EVENT: 'postmam.remoteRetriggerEvent',
MODAL_LOADED: 'postmam.modalPrompt.loaded',
MODAL_PROMPT_ACCEPTED: 'postmam.modalPrompt.accepted',
MODAL_PROMPT_REJECTED: 'postmam.modalPrompt.canceled',
POPUP_LOADED: 'postmam.popup.loaded',
POPUP_ACCEPTED: 'postmam.popup.accepted',
POPUP_REJECTED: 'postmam.popup.canceled',
POPUP_CLOSING: 'postman.popup.closing',
REMOTE_NOTIFICATION_PERMISSION_CHANGED: 'postmam.remoteNotificationPermissionChanged',
IFRAME_POPUP_INITIALIZE: 'postmam.iframePopupInitialize',
UNSUBSCRIBE_FROM_PUSH: 'postmam.unsubscribeFromPush',
SET_SESSION_COUNT: 'postmam.setSessionCount',
REQUEST_HOST_URL: 'postmam.requestHostUrl',
WINDOW_TIMEOUT: 'postmam.windowTimeout',
FINISH_REMOTE_REGISTRATION: 'postmam.finishRemoteRegistration',
FINISH_REMOTE_REGISTRATION_IN_PROGRESS: 'postmam.finishRemoteRegistrationInProgress',
POPUP_BEGIN_MESSAGEPORT_COMMS: 'postmam.beginMessagePortComms',
SERVICEWORKER_COMMAND_REDIRECT: 'postmam.command.redirect',
MARK_PROMPT_DISMISSED: 'postmam.markPromptDismissed',
IS_SUBSCRIBED: 'postmam.isSubscribed',
UNSUBSCRIBE_PROXY_FRAME: 'postman.unsubscribeProxyFrame',
GET_EVENT_LISTENER_COUNT: 'postmam.getEventListenerCount',
SERVICE_WORKER_STATE: 'postmam.serviceWorkerState',
GET_WORKER_VERSION: 'postmam.getWorkerVersion',
SUBSCRIPTION_EXPIRATION_STATE: 'postmam.subscriptionExpirationState',
PROCESS_EXPIRING_SUBSCRIPTIONS: 'postmam.processExpiringSubscriptions',
GET_SUBSCRIPTION_STATE: 'postmam.getSubscriptionState',
};
static EVENTS = {
/**
* Occurs when the user clicks the "Continue" or "No Thanks" button on the HTTP popup or HTTPS modal prompt.
* For HTTP sites (and HTTPS sites using the modal prompt), this event is fired before the native permission
* prompt is shown. This event is mostly used for HTTP sites.
*/
CUSTOM_PROMPT_CLICKED: 'customPromptClick',
/**
* Occurs when the user clicks "Allow" or "Block" on the native permission prompt on Chrome, Firefox, or Safari.
* This event is used for both HTTP and HTTPS sites and occurs after the user actually grants notification
* permissions for the site. Occurs before the user is actually subscribed to push notifications.
*/
NATIVE_PROMPT_PERMISSIONCHANGED: 'notificationPermissionChange',
/**
* Occurs after the user is officially subscribed to push notifications. The service worker is fully registered
* and activated and the user is eligible to receive push notifications at any point after this.
*/
SUBSCRIPTION_CHANGED: 'subscriptionChange',
/**
* Occurs after a POST call to OneSignal's server to send the welcome notification has completed. The actual