@onesignal/onesignal-vue3 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -120,16 +120,16 @@ const isPushSupported = (): boolean => {
120
120
  return isPushNotificationsSupported();
121
121
  }
122
122
 
123
- interface AutoPromptOptions { force?: boolean; forceSlidedownOverNative?: boolean; slidedownPromptOptions?: IOneSignalAutoPromptOptions; }
124
- interface IOneSignalAutoPromptOptions { force?: boolean; forceSlidedownOverNative?: boolean; isInUpdateMode?: boolean; categoryOptions?: IOneSignalCategories; }
125
- interface IOneSignalCategories { positiveUpdateButton: string; negativeUpdateButton: string; savingButtonText: string; errorButtonText: string; updateMessage: string; tags: IOneSignalTagCategory[]; }
126
- interface IOneSignalTagCategory { tag: string; label: string; checked?: boolean; }
127
- type PushSubscriptionNamespaceProperties = { id: string | null | undefined; token: string | null | undefined; optedIn: boolean; };
128
- type SubscriptionChangeEvent = { previous: PushSubscriptionNamespaceProperties; current: PushSubscriptionNamespaceProperties; };
129
- type NotificationEventName = 'click' | 'foregroundWillDisplay' | 'dismiss' | 'permissionChange' | 'permissionPromptDisplay';
130
- type SlidedownEventName = 'slidedownShown';
131
- type OneSignalDeferredLoadedCallback = (onesignal: IOneSignalOneSignal) => void;
132
- interface IOSNotification {
123
+ export interface AutoPromptOptions { force?: boolean; forceSlidedownOverNative?: boolean; slidedownPromptOptions?: IOneSignalAutoPromptOptions; }
124
+ export interface IOneSignalAutoPromptOptions { force?: boolean; forceSlidedownOverNative?: boolean; isInUpdateMode?: boolean; categoryOptions?: IOneSignalCategories; }
125
+ export interface IOneSignalCategories { positiveUpdateButton: string; negativeUpdateButton: string; savingButtonText: string; errorButtonText: string; updateMessage: string; tags: IOneSignalTagCategory[]; }
126
+ export interface IOneSignalTagCategory { tag: string; label: string; checked?: boolean; }
127
+ export type PushSubscriptionNamespaceProperties = { id: string | null | undefined; token: string | null | undefined; optedIn: boolean; };
128
+ export type SubscriptionChangeEvent = { previous: PushSubscriptionNamespaceProperties; current: PushSubscriptionNamespaceProperties; };
129
+ export type NotificationEventName = 'click' | 'foregroundWillDisplay' | 'dismiss' | 'permissionChange' | 'permissionPromptDisplay';
130
+ export type SlidedownEventName = 'slidedownShown';
131
+ export type OneSignalDeferredLoadedCallback = (onesignal: IOneSignalOneSignal) => void;
132
+ export interface IOSNotification {
133
133
  /**
134
134
  * The OneSignal notification id;
135
135
  * - Primary id on OneSignal's REST API and dashboard
@@ -171,7 +171,7 @@ interface IOSNotification {
171
171
  * If this value is the same as existing notification, it will replace it
172
172
  * Can be set when creating the notification with "Web Push Topic" on the dashboard
173
173
  * or web_push_topic from the REST API.
174
- */
174
+ */
175
175
  readonly topic?: string;
176
176
 
177
177
  /**
@@ -191,7 +191,7 @@ interface IOSNotification {
191
191
  readonly confirmDelivery: boolean;
192
192
  }
193
193
 
194
- interface IOSNotificationActionButton {
194
+ export interface IOSNotificationActionButton {
195
195
  /**
196
196
  * Any unique identifier to represent which button was clicked. This is typically passed back to the service worker
197
197
  * and host page through events to identify which button was clicked.
@@ -212,56 +212,246 @@ interface IOSNotificationActionButton {
212
212
  readonly launchURL?: string;
213
213
  }
214
214
 
215
- interface NotificationClickResult {
215
+ export interface NotificationClickResult {
216
216
  readonly actionId?: string;
217
217
  readonly url?: string;
218
218
  }
219
219
 
220
- type NotificationEventTypeMap = {
220
+ export type NotificationEventTypeMap = {
221
221
  'click': NotificationClickEvent;
222
222
  'foregroundWillDisplay': NotificationForegroundWillDisplayEvent;
223
223
  'dismiss': NotificationDismissEvent;
224
224
  'permissionChange': boolean;
225
225
  'permissionPromptDisplay': void;
226
- }
226
+ };
227
227
 
228
- interface NotificationForegroundWillDisplayEvent {
228
+ export interface NotificationForegroundWillDisplayEvent {
229
229
  readonly notification: IOSNotification;
230
230
  preventDefault(): void;
231
231
  }
232
232
 
233
- interface NotificationDismissEvent {
233
+ export interface NotificationDismissEvent {
234
234
  notification: IOSNotification;
235
235
  }
236
236
 
237
- interface NotificationClickEvent {
237
+ export interface NotificationClickEvent {
238
238
  readonly notification: IOSNotification;
239
239
  readonly result: NotificationClickResult;
240
240
  }
241
241
 
242
+ export type UserChangeEvent = {
243
+ current: UserNamespaceProperties;
244
+ };
245
+ export type UserNamespaceProperties = {
246
+ onesignalId: string | undefined;
247
+ externalId: string | undefined;
248
+ };
242
249
 
243
- interface IInitObject {
250
+ export interface IInitObject {
244
251
  appId: string;
245
252
  subdomainName?: string;
246
253
  requiresUserPrivacyConsent?: boolean;
247
- promptOptions?: object;
248
- welcomeNotification?: object;
249
- notifyButton?: object;
254
+ promptOptions?: {
255
+ slidedown: {
256
+ prompts: {
257
+ /**
258
+ * Whether to automatically display the prompt.
259
+ * `true` will display the prompt based on the delay options.
260
+ * `false` will prevent the prompt from displaying until the Slidedowns methods are used.
261
+ */
262
+ autoPrompt: boolean;
263
+
264
+ /**
265
+ * Only available for type: category. Up to 10 categories.
266
+ * @example
267
+ * categories: [{ tag: 'local_news', label: 'Local News' }] // The user will be tagged with local_news but will see "Local News" in the prompt.
268
+ */
269
+ categories: {
270
+ /** Should identify the action. */
271
+ tag: string;
272
+
273
+ /** What the user will see. */
274
+ label: string;
275
+ }[];
276
+
277
+ /**
278
+ * The delay options for the prompt.
279
+ * @example delay: { pageViews: 3, timeDelay: 20 } // The user will not be shown the prompt until 20 seconds after the 3rd page view.
280
+ */
281
+ delay: {
282
+ /** The number of pages a user needs to visit before the prompt is displayed. */
283
+ pageViews?: number;
284
+
285
+ /** The number of seconds a user needs to wait before the prompt is displayed.Both options must be satisfied for the prompt to display */
286
+ timeDelay?: number;
287
+ };
288
+
289
+ /**
290
+ * The text to display in the prompt.
291
+ */
292
+ text?: {
293
+ /** The callout asking the user to opt-in. Up to 90 characters. */
294
+ actionMessage?: string;
295
+
296
+ /** Triggers the opt-in. Up to 15 characters. */
297
+ acceptButton?: string;
298
+
299
+ /** Cancels opt-in. Up to 15 characters. */
300
+ cancelMessage?: string;
301
+
302
+ /** The message of the confirmation prompt displayed after the email and/or phone number is provided. Up to 90 characters. */
303
+ confirmMessage?: string;
304
+
305
+ /** Identifies the email text field. Up to 15 characters. */
306
+ emailLabel?: string;
307
+
308
+ /** Cancels the category update. Up to 15 characters. */
309
+ negativeUpdateButton?: string;
310
+
311
+ /** Saves the updated category tags. Up to 15 characters. */
312
+ positiveUpdateButton?: string;
313
+
314
+ /** Identifies the phone number text field. Up to 15 characters. */
315
+ smsLabel?: string;
316
+
317
+ /** A different message shown to subscribers presented the prompt again to update categories. Up to 90 characters. */
318
+ updateMessage?: string;
319
+ };
320
+
321
+ /**
322
+ * The type of prompt to display.
323
+ * `push` which is the Slide Prompt without categories.
324
+ * `category` which is the Slide Prompt with categories.
325
+ * `sms` only asks for phone number.
326
+ * `email` only asks for email address.
327
+ * `smsAndEmail` asks for both phone number and email address.
328
+ */
329
+ type: 'push' | 'category' | 'sms' | 'email' | 'smsAndEmail';
330
+ }[];
331
+ };
332
+ };
333
+ welcomeNotification?: {
334
+ /**
335
+ * Disables sending a welcome notification to new site visitors. If you want to disable welcome notifications, this is the only option you need.
336
+ */
337
+ disabled?: boolean;
338
+
339
+ /**
340
+ * The welcome notification's message. You can localize this to your own language.
341
+ * If left blank or set to blank, the default of 'Thanks for subscribing!' will be used.
342
+ */
343
+ message: string;
344
+
345
+ /**
346
+ * The welcome notification's title. You can localize this to your own language. If not set, or left blank, the site's title will be used.
347
+ * Set to one space ' ' to clear the title, although this is not recommended.
348
+ */
349
+ title?: string;
350
+
351
+ /**
352
+ * By default, clicking the welcome notification does not open any link.
353
+ * This is recommended because the user has just visited your site and subscribed.
354
+ */
355
+ url: string;
356
+ };
357
+
358
+ /**
359
+ * Will enable customization of the notify/subscription bell button.
360
+ */
361
+ notifyButton?: {
362
+ /**
363
+ * A function you define that returns true to show the Subscription Bell, or false to hide it.
364
+ * Typically used the hide the Subscription Bell after the user is subscribed.
365
+ * This function is not re-evaluated on every state change; this function is only evaluated once when the Subscription Bell begins to show.
366
+ */
367
+ displayPredicate?: () => boolean | Promise<boolean>;
368
+
369
+ /**
370
+ * Enable the Subscription Bell. The Subscription Bell is otherwise disabled by default.
371
+ */
372
+ enable?: boolean;
373
+
374
+ /** Specify CSS-valid pixel offsets using bottom, left, and right. */
375
+ offset?: { bottom: string; left: string; right: string };
376
+
377
+ /**
378
+ * If `true`, the Subscription Bell will display an icon that there is 1 unread message.
379
+ * When hovering over the Subscription Bell, the user will see custom text set by message.prenotify.
380
+ */
381
+ prenotify: boolean;
382
+
383
+ /** Either `bottom-left` or `bottom-right`. The Subscription Bell will be fixed at this location on your page. */
384
+ position?: 'bottom-left' | 'bottom-right';
385
+
386
+ /** Set `false` to hide the 'Powered by OneSignal' text in the Subscription Bell dialog popup. */
387
+ showCredit: boolean;
388
+
389
+ /**
390
+ * The Subscription Bell will initially appear at one of these sizes, and then shrink down to size `small` after the user subscribes.
391
+ */
392
+ size?: 'small' | 'medium' | 'large';
393
+
394
+ /** Customize the Subscription Bell text. */
395
+ text: {
396
+ 'dialog.blocked.message': string;
397
+ 'dialog.blocked.title': string;
398
+ 'dialog.main.button.subscribe': string;
399
+ 'dialog.main.button.unsubscribe': string;
400
+ 'dialog.main.title': string;
401
+ 'message.action.resubscribed': string;
402
+ 'message.action.subscribed': string;
403
+ 'message.action.subscribing': string;
404
+ 'message.action.unsubscribed': string;
405
+ 'message.prenotify': string;
406
+ 'tip.state.blocked': string;
407
+ 'tip.state.subscribed': string;
408
+ 'tip.state.unsubscribed': string;
409
+ };
410
+ };
411
+
250
412
  persistNotification?: boolean;
251
- webhooks?: object;
413
+ webhooks?: {
414
+ /**
415
+ * Enable this setting only if your server has CORS enabled and supports non-simple CORS requests.
416
+ * If this setting is disabled, your webhook will not need CORS to receive data, but it will not receive the custom headers.
417
+ * The simplest option is to leave it disabled.
418
+ * @default false
419
+ */
420
+ cors: boolean;
421
+
422
+ /**
423
+ * This event occurs after a notification is clicked.
424
+ * @example https://site.com/hook
425
+ */
426
+ 'notification.clicked'?: string;
427
+
428
+ /**
429
+ * This event occurs after a notification is intentionally dismissed by the user (clicking the notification body or one of the notification action buttons does not trigger the dismissed webhook),
430
+ * after a group of notifications are all dismissed (with this notification as part of that group), or after a notification expires on its own time and disappears. This event is supported on Chrome only.
431
+ * @example https://site.com/hook
432
+ */
433
+ 'notification.dismissed'?: string;
434
+
435
+ /**
436
+ * This event occurs after a notification is displayed.
437
+ * @example https://site.com/hook
438
+ */
439
+ 'notification.willDisplay'?: string;
440
+ };
252
441
  autoResubscribe?: boolean;
253
442
  autoRegister?: boolean;
254
443
  notificationClickHandlerMatch?: string;
255
444
  notificationClickHandlerAction?: string;
445
+ path?: string;
256
446
  serviceWorkerParam?: { scope: string };
257
447
  serviceWorkerPath?: string;
448
+ serviceWorkerOverrideForTypical?: boolean;
258
449
  serviceWorkerUpdaterPath?: string;
259
- path?: string;
260
450
  allowLocalhostAsSecureOrigin?: boolean;
261
451
  [key: string]: any;
262
452
  }
263
453
 
264
- interface IOneSignalOneSignal {
454
+ export interface IOneSignalOneSignal {
265
455
  Slidedown: IOneSignalSlidedown;
266
456
  Notifications: IOneSignalNotifications;
267
457
  Session: IOneSignalSession;
@@ -273,7 +463,7 @@ interface IOneSignalOneSignal {
273
463
  setConsentGiven(consent: boolean): Promise<void>;
274
464
  setConsentRequired(requiresConsent: boolean): Promise<void>;
275
465
  }
276
- interface IOneSignalNotifications {
466
+ export interface IOneSignalNotifications {
277
467
  permissionNative: NotificationPermission;
278
468
  permission: boolean;
279
469
  setDefaultUrl(url: string): Promise<void>;
@@ -283,7 +473,7 @@ interface IOneSignalNotifications {
283
473
  addEventListener<K extends NotificationEventName>(event: K, listener: (obj: NotificationEventTypeMap[K]) => void): void;
284
474
  removeEventListener<K extends NotificationEventName>(event: K, listener: (obj: NotificationEventTypeMap[K]) => void): void;
285
475
  }
286
- interface IOneSignalSlidedown {
476
+ export interface IOneSignalSlidedown {
287
477
  promptPush(options?: AutoPromptOptions): Promise<void>;
288
478
  promptPushCategories(options?: AutoPromptOptions): Promise<void>;
289
479
  promptSms(options?: AutoPromptOptions): Promise<void>;
@@ -292,14 +482,16 @@ interface IOneSignalSlidedown {
292
482
  addEventListener(event: SlidedownEventName, listener: (wasShown: boolean) => void): void;
293
483
  removeEventListener(event: SlidedownEventName, listener: (wasShown: boolean) => void): void;
294
484
  }
295
- interface IOneSignalDebug {
485
+ export interface IOneSignalDebug {
296
486
  setLogLevel(logLevel: string): void;
297
487
  }
298
- interface IOneSignalSession {
488
+ export interface IOneSignalSession {
299
489
  sendOutcome(outcomeName: string, outcomeWeight?: number): Promise<void>;
300
490
  sendUniqueOutcome(outcomeName: string): Promise<void>;
301
491
  }
302
- interface IOneSignalUser {
492
+ export interface IOneSignalUser {
493
+ onesignalId: string | undefined;
494
+ externalId: string | undefined;
303
495
  PushSubscription: IOneSignalPushSubscription;
304
496
  addAlias(label: string, id: string): void;
305
497
  addAliases(aliases: { [key: string]: string }): void;
@@ -313,8 +505,13 @@ interface IOneSignalUser {
313
505
  addTags(tags: { [key: string]: string }): void;
314
506
  removeTag(key: string): void;
315
507
  removeTags(keys: string[]): void;
508
+ getTags(): { [key: string]: string };
509
+ addEventListener(event: 'change', listener: (change: UserChangeEvent) => void): void;
510
+ removeEventListener(event: 'change', listener: (change: UserChangeEvent) => void): void;
511
+ setLanguage(language: string): void;
512
+ getLanguage(): string;
316
513
  }
317
- interface IOneSignalPushSubscription {
514
+ export interface IOneSignalPushSubscription {
318
515
  id: string | null | undefined;
319
516
  token: string | null | undefined;
320
517
  optedIn: boolean | undefined;
@@ -323,17 +520,16 @@ interface IOneSignalPushSubscription {
323
520
  addEventListener(event: 'change', listener: (change: SubscriptionChangeEvent) => void): void;
324
521
  removeEventListener(event: 'change', listener: (change: SubscriptionChangeEvent) => void): void;
325
522
  }
326
-
327
523
  function oneSignalLogin(externalId: string, jwtToken?: string): Promise<void> {
328
524
  return new Promise(function (resolve, reject) {
329
525
  if (isOneSignalScriptFailed) {
330
- reject();
526
+ reject(new Error('OneSignal script failed to load.'));
527
+ return;
331
528
  }
332
529
 
333
530
  try {
334
531
  window.OneSignalDeferred?.push((OneSignal) => {
335
- OneSignal.login(externalId, jwtToken)
336
- .then(value => resolve(value))
532
+ OneSignal.login(externalId, jwtToken).then(() => resolve())
337
533
  .catch(error => reject(error));
338
534
  });
339
535
  } catch (error) {
@@ -341,17 +537,16 @@ function oneSignalLogin(externalId: string, jwtToken?: string): Promise<void> {
341
537
  }
342
538
  });
343
539
  }
344
-
345
540
  function oneSignalLogout(): Promise<void> {
346
541
  return new Promise(function (resolve, reject) {
347
542
  if (isOneSignalScriptFailed) {
348
- reject();
543
+ reject(new Error('OneSignal script failed to load.'));
544
+ return;
349
545
  }
350
546
 
351
547
  try {
352
548
  window.OneSignalDeferred?.push((OneSignal) => {
353
- OneSignal.logout()
354
- .then(value => resolve(value))
549
+ OneSignal.logout().then(() => resolve())
355
550
  .catch(error => reject(error));
356
551
  });
357
552
  } catch (error) {
@@ -359,17 +554,16 @@ function oneSignalLogout(): Promise<void> {
359
554
  }
360
555
  });
361
556
  }
362
-
363
557
  function oneSignalSetConsentGiven(consent: boolean): Promise<void> {
364
558
  return new Promise(function (resolve, reject) {
365
559
  if (isOneSignalScriptFailed) {
366
- reject();
560
+ reject(new Error('OneSignal script failed to load.'));
561
+ return;
367
562
  }
368
563
 
369
564
  try {
370
565
  window.OneSignalDeferred?.push((OneSignal) => {
371
- OneSignal.setConsentGiven(consent)
372
- .then(value => resolve(value))
566
+ OneSignal.setConsentGiven(consent).then(() => resolve())
373
567
  .catch(error => reject(error));
374
568
  });
375
569
  } catch (error) {
@@ -377,17 +571,16 @@ function oneSignalSetConsentGiven(consent: boolean): Promise<void> {
377
571
  }
378
572
  });
379
573
  }
380
-
381
574
  function oneSignalSetConsentRequired(requiresConsent: boolean): Promise<void> {
382
575
  return new Promise(function (resolve, reject) {
383
576
  if (isOneSignalScriptFailed) {
384
- reject();
577
+ reject(new Error('OneSignal script failed to load.'));
578
+ return;
385
579
  }
386
580
 
387
581
  try {
388
582
  window.OneSignalDeferred?.push((OneSignal) => {
389
- OneSignal.setConsentRequired(requiresConsent)
390
- .then(value => resolve(value))
583
+ OneSignal.setConsentRequired(requiresConsent).then(() => resolve())
391
584
  .catch(error => reject(error));
392
585
  });
393
586
  } catch (error) {
@@ -395,17 +588,16 @@ function oneSignalSetConsentRequired(requiresConsent: boolean): Promise<void> {
395
588
  }
396
589
  });
397
590
  }
398
-
399
591
  function slidedownPromptPush(options?: AutoPromptOptions): Promise<void> {
400
592
  return new Promise(function (resolve, reject) {
401
593
  if (isOneSignalScriptFailed) {
402
- reject();
594
+ reject(new Error('OneSignal script failed to load.'));
595
+ return;
403
596
  }
404
597
 
405
598
  try {
406
599
  window.OneSignalDeferred?.push((OneSignal) => {
407
- OneSignal.Slidedown.promptPush(options)
408
- .then(value => resolve(value))
600
+ OneSignal.Slidedown.promptPush(options).then(() => resolve())
409
601
  .catch(error => reject(error));
410
602
  });
411
603
  } catch (error) {
@@ -413,17 +605,16 @@ function slidedownPromptPush(options?: AutoPromptOptions): Promise<void> {
413
605
  }
414
606
  });
415
607
  }
416
-
417
608
  function slidedownPromptPushCategories(options?: AutoPromptOptions): Promise<void> {
418
609
  return new Promise(function (resolve, reject) {
419
610
  if (isOneSignalScriptFailed) {
420
- reject();
611
+ reject(new Error('OneSignal script failed to load.'));
612
+ return;
421
613
  }
422
614
 
423
615
  try {
424
616
  window.OneSignalDeferred?.push((OneSignal) => {
425
- OneSignal.Slidedown.promptPushCategories(options)
426
- .then(value => resolve(value))
617
+ OneSignal.Slidedown.promptPushCategories(options).then(() => resolve())
427
618
  .catch(error => reject(error));
428
619
  });
429
620
  } catch (error) {
@@ -431,17 +622,16 @@ function slidedownPromptPushCategories(options?: AutoPromptOptions): Promise<voi
431
622
  }
432
623
  });
433
624
  }
434
-
435
625
  function slidedownPromptSms(options?: AutoPromptOptions): Promise<void> {
436
626
  return new Promise(function (resolve, reject) {
437
627
  if (isOneSignalScriptFailed) {
438
- reject();
628
+ reject(new Error('OneSignal script failed to load.'));
629
+ return;
439
630
  }
440
631
 
441
632
  try {
442
633
  window.OneSignalDeferred?.push((OneSignal) => {
443
- OneSignal.Slidedown.promptSms(options)
444
- .then(value => resolve(value))
634
+ OneSignal.Slidedown.promptSms(options).then(() => resolve())
445
635
  .catch(error => reject(error));
446
636
  });
447
637
  } catch (error) {
@@ -449,17 +639,16 @@ function slidedownPromptSms(options?: AutoPromptOptions): Promise<void> {
449
639
  }
450
640
  });
451
641
  }
452
-
453
642
  function slidedownPromptEmail(options?: AutoPromptOptions): Promise<void> {
454
643
  return new Promise(function (resolve, reject) {
455
644
  if (isOneSignalScriptFailed) {
456
- reject();
645
+ reject(new Error('OneSignal script failed to load.'));
646
+ return;
457
647
  }
458
648
 
459
649
  try {
460
650
  window.OneSignalDeferred?.push((OneSignal) => {
461
- OneSignal.Slidedown.promptEmail(options)
462
- .then(value => resolve(value))
651
+ OneSignal.Slidedown.promptEmail(options).then(() => resolve())
463
652
  .catch(error => reject(error));
464
653
  });
465
654
  } catch (error) {
@@ -467,17 +656,16 @@ function slidedownPromptEmail(options?: AutoPromptOptions): Promise<void> {
467
656
  }
468
657
  });
469
658
  }
470
-
471
659
  function slidedownPromptSmsAndEmail(options?: AutoPromptOptions): Promise<void> {
472
660
  return new Promise(function (resolve, reject) {
473
661
  if (isOneSignalScriptFailed) {
474
- reject();
662
+ reject(new Error('OneSignal script failed to load.'));
663
+ return;
475
664
  }
476
665
 
477
666
  try {
478
667
  window.OneSignalDeferred?.push((OneSignal) => {
479
- OneSignal.Slidedown.promptSmsAndEmail(options)
480
- .then(value => resolve(value))
668
+ OneSignal.Slidedown.promptSmsAndEmail(options).then(() => resolve())
481
669
  .catch(error => reject(error));
482
670
  });
483
671
  } catch (error) {
@@ -485,29 +673,30 @@ function slidedownPromptSmsAndEmail(options?: AutoPromptOptions): Promise<void>
485
673
  }
486
674
  });
487
675
  }
488
-
489
676
  function slidedownAddEventListener(event: SlidedownEventName, listener: (wasShown: boolean) => void): void {
677
+
490
678
  window.OneSignalDeferred?.push((OneSignal) => {
491
- OneSignal.Slidedown.addEventListener(event, listener)
679
+ OneSignal.Slidedown.addEventListener(event, listener);
492
680
  });
681
+
493
682
  }
494
-
495
683
  function slidedownRemoveEventListener(event: SlidedownEventName, listener: (wasShown: boolean) => void): void {
684
+
496
685
  window.OneSignalDeferred?.push((OneSignal) => {
497
- OneSignal.Slidedown.removeEventListener(event, listener)
686
+ OneSignal.Slidedown.removeEventListener(event, listener);
498
687
  });
688
+
499
689
  }
500
-
501
690
  function notificationsSetDefaultUrl(url: string): Promise<void> {
502
691
  return new Promise(function (resolve, reject) {
503
692
  if (isOneSignalScriptFailed) {
504
- reject();
693
+ reject(new Error('OneSignal script failed to load.'));
694
+ return;
505
695
  }
506
696
 
507
697
  try {
508
698
  window.OneSignalDeferred?.push((OneSignal) => {
509
- OneSignal.Notifications.setDefaultUrl(url)
510
- .then(value => resolve(value))
699
+ OneSignal.Notifications.setDefaultUrl(url).then(() => resolve())
511
700
  .catch(error => reject(error));
512
701
  });
513
702
  } catch (error) {
@@ -515,17 +704,16 @@ function notificationsSetDefaultUrl(url: string): Promise<void> {
515
704
  }
516
705
  });
517
706
  }
518
-
519
707
  function notificationsSetDefaultTitle(title: string): Promise<void> {
520
708
  return new Promise(function (resolve, reject) {
521
709
  if (isOneSignalScriptFailed) {
522
- reject();
710
+ reject(new Error('OneSignal script failed to load.'));
711
+ return;
523
712
  }
524
713
 
525
714
  try {
526
715
  window.OneSignalDeferred?.push((OneSignal) => {
527
- OneSignal.Notifications.setDefaultTitle(title)
528
- .then(value => resolve(value))
716
+ OneSignal.Notifications.setDefaultTitle(title).then(() => resolve())
529
717
  .catch(error => reject(error));
530
718
  });
531
719
  } catch (error) {
@@ -533,17 +721,16 @@ function notificationsSetDefaultTitle(title: string): Promise<void> {
533
721
  }
534
722
  });
535
723
  }
536
-
537
724
  function notificationsRequestPermission(): Promise<void> {
538
725
  return new Promise(function (resolve, reject) {
539
726
  if (isOneSignalScriptFailed) {
540
- reject();
727
+ reject(new Error('OneSignal script failed to load.'));
728
+ return;
541
729
  }
542
730
 
543
731
  try {
544
732
  window.OneSignalDeferred?.push((OneSignal) => {
545
- OneSignal.Notifications.requestPermission()
546
- .then(value => resolve(value))
733
+ OneSignal.Notifications.requestPermission().then(() => resolve())
547
734
  .catch(error => reject(error));
548
735
  });
549
736
  } catch (error) {
@@ -551,29 +738,30 @@ function notificationsRequestPermission(): Promise<void> {
551
738
  }
552
739
  });
553
740
  }
554
-
555
741
  function notificationsAddEventListener<K extends NotificationEventName>(event: K, listener: (obj: NotificationEventTypeMap[K]) => void): void {
742
+
556
743
  window.OneSignalDeferred?.push((OneSignal) => {
557
- OneSignal.Notifications.addEventListener(event, listener)
744
+ OneSignal.Notifications.addEventListener(event, listener);
558
745
  });
746
+
559
747
  }
560
-
561
748
  function notificationsRemoveEventListener<K extends NotificationEventName>(event: K, listener: (obj: NotificationEventTypeMap[K]) => void): void {
749
+
562
750
  window.OneSignalDeferred?.push((OneSignal) => {
563
- OneSignal.Notifications.removeEventListener(event, listener)
751
+ OneSignal.Notifications.removeEventListener(event, listener);
564
752
  });
753
+
565
754
  }
566
-
567
755
  function sessionSendOutcome(outcomeName: string, outcomeWeight?: number): Promise<void> {
568
756
  return new Promise(function (resolve, reject) {
569
757
  if (isOneSignalScriptFailed) {
570
- reject();
758
+ reject(new Error('OneSignal script failed to load.'));
759
+ return;
571
760
  }
572
761
 
573
762
  try {
574
763
  window.OneSignalDeferred?.push((OneSignal) => {
575
- OneSignal.Session.sendOutcome(outcomeName, outcomeWeight)
576
- .then(value => resolve(value))
764
+ OneSignal.Session.sendOutcome(outcomeName, outcomeWeight).then(() => resolve())
577
765
  .catch(error => reject(error));
578
766
  });
579
767
  } catch (error) {
@@ -581,17 +769,16 @@ function sessionSendOutcome(outcomeName: string, outcomeWeight?: number): Promis
581
769
  }
582
770
  });
583
771
  }
584
-
585
772
  function sessionSendUniqueOutcome(outcomeName: string): Promise<void> {
586
773
  return new Promise(function (resolve, reject) {
587
774
  if (isOneSignalScriptFailed) {
588
- reject();
775
+ reject(new Error('OneSignal script failed to load.'));
776
+ return;
589
777
  }
590
778
 
591
779
  try {
592
780
  window.OneSignalDeferred?.push((OneSignal) => {
593
- OneSignal.Session.sendUniqueOutcome(outcomeName)
594
- .then(value => resolve(value))
781
+ OneSignal.Session.sendUniqueOutcome(outcomeName).then(() => resolve())
595
782
  .catch(error => reject(error));
596
783
  });
597
784
  } catch (error) {
@@ -599,89 +786,135 @@ function sessionSendUniqueOutcome(outcomeName: string): Promise<void> {
599
786
  }
600
787
  });
601
788
  }
602
-
603
789
  function userAddAlias(label: string, id: string): void {
790
+
604
791
  window.OneSignalDeferred?.push((OneSignal) => {
605
- OneSignal.User.addAlias(label, id)
792
+ OneSignal.User.addAlias(label, id);
606
793
  });
794
+
607
795
  }
608
-
609
796
  function userAddAliases(aliases: { [key: string]: string }): void {
797
+
610
798
  window.OneSignalDeferred?.push((OneSignal) => {
611
- OneSignal.User.addAliases(aliases)
799
+ OneSignal.User.addAliases(aliases);
612
800
  });
801
+
613
802
  }
614
-
615
803
  function userRemoveAlias(label: string): void {
804
+
616
805
  window.OneSignalDeferred?.push((OneSignal) => {
617
- OneSignal.User.removeAlias(label)
806
+ OneSignal.User.removeAlias(label);
618
807
  });
808
+
619
809
  }
620
-
621
810
  function userRemoveAliases(labels: string[]): void {
811
+
622
812
  window.OneSignalDeferred?.push((OneSignal) => {
623
- OneSignal.User.removeAliases(labels)
813
+ OneSignal.User.removeAliases(labels);
624
814
  });
815
+
625
816
  }
626
-
627
817
  function userAddEmail(email: string): void {
818
+
628
819
  window.OneSignalDeferred?.push((OneSignal) => {
629
- OneSignal.User.addEmail(email)
820
+ OneSignal.User.addEmail(email);
630
821
  });
822
+
631
823
  }
632
-
633
824
  function userRemoveEmail(email: string): void {
825
+
634
826
  window.OneSignalDeferred?.push((OneSignal) => {
635
- OneSignal.User.removeEmail(email)
827
+ OneSignal.User.removeEmail(email);
636
828
  });
829
+
637
830
  }
638
-
639
831
  function userAddSms(smsNumber: string): void {
832
+
640
833
  window.OneSignalDeferred?.push((OneSignal) => {
641
- OneSignal.User.addSms(smsNumber)
834
+ OneSignal.User.addSms(smsNumber);
642
835
  });
836
+
643
837
  }
644
-
645
838
  function userRemoveSms(smsNumber: string): void {
839
+
646
840
  window.OneSignalDeferred?.push((OneSignal) => {
647
- OneSignal.User.removeSms(smsNumber)
841
+ OneSignal.User.removeSms(smsNumber);
648
842
  });
843
+
649
844
  }
650
-
651
845
  function userAddTag(key: string, value: string): void {
846
+
652
847
  window.OneSignalDeferred?.push((OneSignal) => {
653
- OneSignal.User.addTag(key, value)
848
+ OneSignal.User.addTag(key, value);
654
849
  });
850
+
655
851
  }
656
-
657
852
  function userAddTags(tags: { [key: string]: string }): void {
853
+
658
854
  window.OneSignalDeferred?.push((OneSignal) => {
659
- OneSignal.User.addTags(tags)
855
+ OneSignal.User.addTags(tags);
660
856
  });
857
+
661
858
  }
662
-
663
859
  function userRemoveTag(key: string): void {
860
+
664
861
  window.OneSignalDeferred?.push((OneSignal) => {
665
- OneSignal.User.removeTag(key)
862
+ OneSignal.User.removeTag(key);
666
863
  });
864
+
667
865
  }
668
-
669
866
  function userRemoveTags(keys: string[]): void {
867
+
670
868
  window.OneSignalDeferred?.push((OneSignal) => {
671
- OneSignal.User.removeTags(keys)
869
+ OneSignal.User.removeTags(keys);
672
870
  });
871
+
872
+ }
873
+ function userGetTags(): { [key: string]: string } {
874
+ let retVal: { [key: string]: string };
875
+ window.OneSignalDeferred?.push((OneSignal) => {
876
+ retVal = OneSignal.User.getTags();
877
+ });
878
+ return retVal;
879
+ }
880
+ function userAddEventListener(event: 'change', listener: (change: UserChangeEvent) => void): void {
881
+
882
+ window.OneSignalDeferred?.push((OneSignal) => {
883
+ OneSignal.User.addEventListener(event, listener);
884
+ });
885
+
886
+ }
887
+ function userRemoveEventListener(event: 'change', listener: (change: UserChangeEvent) => void): void {
888
+
889
+ window.OneSignalDeferred?.push((OneSignal) => {
890
+ OneSignal.User.removeEventListener(event, listener);
891
+ });
892
+
893
+ }
894
+ function userSetLanguage(language: string): void {
895
+
896
+ window.OneSignalDeferred?.push((OneSignal) => {
897
+ OneSignal.User.setLanguage(language);
898
+ });
899
+
900
+ }
901
+ function userGetLanguage(): string {
902
+ let retVal: string;
903
+ window.OneSignalDeferred?.push((OneSignal) => {
904
+ retVal = OneSignal.User.getLanguage();
905
+ });
906
+ return retVal;
673
907
  }
674
-
675
908
  function pushSubscriptionOptIn(): Promise<void> {
676
909
  return new Promise(function (resolve, reject) {
677
910
  if (isOneSignalScriptFailed) {
678
- reject();
911
+ reject(new Error('OneSignal script failed to load.'));
912
+ return;
679
913
  }
680
914
 
681
915
  try {
682
916
  window.OneSignalDeferred?.push((OneSignal) => {
683
- OneSignal.User.PushSubscription.optIn()
684
- .then(value => resolve(value))
917
+ OneSignal.User.PushSubscription.optIn().then(() => resolve())
685
918
  .catch(error => reject(error));
686
919
  });
687
920
  } catch (error) {
@@ -689,17 +922,16 @@ function pushSubscriptionOptIn(): Promise<void> {
689
922
  }
690
923
  });
691
924
  }
692
-
693
925
  function pushSubscriptionOptOut(): Promise<void> {
694
926
  return new Promise(function (resolve, reject) {
695
927
  if (isOneSignalScriptFailed) {
696
- reject();
928
+ reject(new Error('OneSignal script failed to load.'));
929
+ return;
697
930
  }
698
931
 
699
932
  try {
700
933
  window.OneSignalDeferred?.push((OneSignal) => {
701
- OneSignal.User.PushSubscription.optOut()
702
- .then(value => resolve(value))
934
+ OneSignal.User.PushSubscription.optOut().then(() => resolve())
703
935
  .catch(error => reject(error));
704
936
  });
705
937
  } catch (error) {
@@ -707,28 +939,31 @@ function pushSubscriptionOptOut(): Promise<void> {
707
939
  }
708
940
  });
709
941
  }
710
-
711
942
  function pushSubscriptionAddEventListener(event: 'change', listener: (change: SubscriptionChangeEvent) => void): void {
943
+
712
944
  window.OneSignalDeferred?.push((OneSignal) => {
713
- OneSignal.User.PushSubscription.addEventListener(event, listener)
945
+ OneSignal.User.PushSubscription.addEventListener(event, listener);
714
946
  });
947
+
715
948
  }
716
-
717
949
  function pushSubscriptionRemoveEventListener(event: 'change', listener: (change: SubscriptionChangeEvent) => void): void {
950
+
718
951
  window.OneSignalDeferred?.push((OneSignal) => {
719
- OneSignal.User.PushSubscription.removeEventListener(event, listener)
952
+ OneSignal.User.PushSubscription.removeEventListener(event, listener);
720
953
  });
954
+
721
955
  }
722
-
723
956
  function debugSetLogLevel(logLevel: string): void {
957
+
724
958
  window.OneSignalDeferred?.push((OneSignal) => {
725
- OneSignal.Debug.setLogLevel(logLevel)
959
+ OneSignal.Debug.setLogLevel(logLevel);
726
960
  });
961
+
727
962
  }
728
963
  const PushSubscriptionNamespace: IOneSignalPushSubscription = {
729
- get id(): string | null | undefined { return window.OneSignal?.User?.PushSubscription?.id },
730
- get token(): string | null | undefined { return window.OneSignal?.User?.PushSubscription?.token },
731
- get optedIn(): boolean | undefined { return window.OneSignal?.User?.PushSubscription?.optedIn },
964
+ get id(): string | null | undefined { return window.OneSignal?.User?.PushSubscription?.id; },
965
+ get token(): string | null | undefined { return window.OneSignal?.User?.PushSubscription?.token; },
966
+ get optedIn(): boolean | undefined { return window.OneSignal?.User?.PushSubscription?.optedIn; },
732
967
  optIn: pushSubscriptionOptIn,
733
968
  optOut: pushSubscriptionOptOut,
734
969
  addEventListener: pushSubscriptionAddEventListener,
@@ -736,6 +971,8 @@ const PushSubscriptionNamespace: IOneSignalPushSubscription = {
736
971
  };
737
972
 
738
973
  const UserNamespace: IOneSignalUser = {
974
+ get onesignalId(): string | undefined { return window.OneSignal?.User?.onesignalId; },
975
+ get externalId(): string | undefined { return window.OneSignal?.User?.externalId; },
739
976
  addAlias: userAddAlias,
740
977
  addAliases: userAddAliases,
741
978
  removeAlias: userRemoveAlias,
@@ -748,6 +985,11 @@ const UserNamespace: IOneSignalUser = {
748
985
  addTags: userAddTags,
749
986
  removeTag: userRemoveTag,
750
987
  removeTags: userRemoveTags,
988
+ getTags: userGetTags,
989
+ addEventListener: userAddEventListener,
990
+ removeEventListener: userRemoveEventListener,
991
+ setLanguage: userSetLanguage,
992
+ getLanguage: userGetLanguage,
751
993
  PushSubscription: PushSubscriptionNamespace,
752
994
  };
753
995