Notifications.ts 3.46 KB
Newer Older
yogevbd's avatar
yogevbd committed
1 2 3 4
import { NativeCommandsSender } from './adapters/NativeCommandsSender';
import { NativeEventsReceiver } from './adapters/NativeEventsReceiver';
import { Commands } from './commands/Commands';
import { EventsRegistry } from './events/EventsRegistry';
5
import { Notification, NotificationCategory } from './interfaces/Notification';
6
import { UniqueIdProvider } from './adapters/UniqueIdProvider';
yogevbd's avatar
yogevbd committed
7
import { CompletionCallbackWrapper } from './adapters/CompletionCallbackWrapper';
yogevbd's avatar
yogevbd committed
8 9 10 11 12 13

export class NotificationsRoot {
  private readonly nativeEventsReceiver: NativeEventsReceiver;
  private readonly nativeCommandsSender: NativeCommandsSender;
  private readonly commands: Commands;
  private readonly eventsRegistry: EventsRegistry;
14
  private readonly uniqueIdProvider: UniqueIdProvider;
yogevbd's avatar
yogevbd committed
15
  private readonly completionCallbackWrapper: CompletionCallbackWrapper;
yogevbd's avatar
yogevbd committed
16 17 18 19

  constructor() {
    this.nativeEventsReceiver = new NativeEventsReceiver();
    this.nativeCommandsSender = new NativeCommandsSender();
yogevbd's avatar
yogevbd committed
20
    this.completionCallbackWrapper = new CompletionCallbackWrapper(this.nativeCommandsSender);
21
    this.uniqueIdProvider = new UniqueIdProvider();
yogevbd's avatar
yogevbd committed
22
    this.commands = new Commands(
23 24
      this.nativeCommandsSender,
      this.uniqueIdProvider
yogevbd's avatar
yogevbd committed
25
    );
yogevbd's avatar
yogevbd committed
26
    this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver, this.completionCallbackWrapper);
yogevbd's avatar
yogevbd committed
27 28 29 30 31
  }

  /**
  * Request permissions to send remote notifications - iOS only
  */
32
  public requestPermissions() {
yogevbd's avatar
yogevbd committed
33 34 35
    return this.commands.requestPermissions();
  }

36 37 38 39 40 41 42
  /**
   * registerPushKit
   */
  public registerPushKit() {
    return this.commands.registerPushKit();
  }
  
yogevbd's avatar
yogevbd committed
43 44 45
  /**
   * Reset the app to a new layout
   */
46 47
  public localNotification(notification: Notification, id: string) {
    return this.commands.sendLocalNotification(notification, id);
yogevbd's avatar
yogevbd committed
48 49 50
  }

  /**
yogevbd's avatar
yogevbd committed
51
   * getInitialNotification
yogevbd's avatar
yogevbd committed
52 53 54 55 56
   */
  public getInitialNotification(): Promise<Notification> {
    return this.commands.getInitialNotification();
  }

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
  /**
   * setCategories
   */
  public setCategories(categories: [NotificationCategory?]) {
    this.commands.setCategories(categories);
  }

  /**
   * getBadgesCount
   */
  public getBadgeCount(): Promise<number> {
    return this.commands.getBadgeCount();
  }

  /**
   * setBadgeCount
   * @param count number of the new badge count
   */
  public setBadgeCount(count: number) {
    return this.commands.setBadgeCount(count);
  }

  /**
   * cancelLocalNotification
  */
  public cancelLocalNotification(notificationId: string) {
    return this.commands.cancelLocalNotification(notificationId);
  }

  /**
   * cancelAllLocalNotifications
   */
  public cancelAllLocalNotifications() {
    this.commands.cancelAllLocalNotifications();
  }

  /**
   * isRegisteredForRemoteNotifications
   */
96 97
  public isRegisteredForRemoteNotifications(): Promise<boolean> {
    return this.commands.isRegisteredForRemoteNotifications();
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  }

  /**
   * checkPermissions
   */
  public checkPermissions() {
    return this.commands.checkPermissions();
  }

  /**
   * removeAllDeliveredNotifications
   */
  public removeAllDeliveredNotifications() {
    return this.commands.removeAllDeliveredNotifications();
  }

  /**
   * removeDeliveredNotifications
   * @param identifiers Array of notification identifiers
   */
  public removeDeliveredNotifications(identifiers: Array<string>) {
    return this.commands.removeDeliveredNotifications(identifiers);
  }

yogevbd's avatar
yogevbd committed
122 123 124 125 126 127 128
  /**
   * Obtain the events registry instance
   */
  public events(): EventsRegistry {
    return this.eventsRegistry;
  }
}