Notifications.ts 2.8 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 } from './DTO/Notification';
6
import { UniqueIdProvider } from './adapters/UniqueIdProvider';
yogevbd's avatar
yogevbd committed
7
import { CompletionCallbackWrapper } from './adapters/CompletionCallbackWrapper';
8
import { NotificationCategory } from './interfaces/NotificationCategory';
9 10
import { NotificationsIOS } from './NotificationsIOS';
import { NotificationsAndroid } from './NotificationsAndroid';
yogevbd's avatar
yogevbd committed
11 12

export class NotificationsRoot {
13 14 15
  public readonly ios: NotificationsIOS;
  public readonly android: NotificationsAndroid;

yogevbd's avatar
yogevbd committed
16 17 18 19
  private readonly nativeEventsReceiver: NativeEventsReceiver;
  private readonly nativeCommandsSender: NativeCommandsSender;
  private readonly commands: Commands;
  private readonly eventsRegistry: EventsRegistry;
20
  private readonly uniqueIdProvider: UniqueIdProvider;
yogevbd's avatar
yogevbd committed
21
  private readonly completionCallbackWrapper: CompletionCallbackWrapper;
yogevbd's avatar
yogevbd committed
22 23 24 25

  constructor() {
    this.nativeEventsReceiver = new NativeEventsReceiver();
    this.nativeCommandsSender = new NativeCommandsSender();
yogevbd's avatar
yogevbd committed
26
    this.completionCallbackWrapper = new CompletionCallbackWrapper(this.nativeCommandsSender);
27
    this.uniqueIdProvider = new UniqueIdProvider();
yogevbd's avatar
yogevbd committed
28
    this.commands = new Commands(
29 30
      this.nativeCommandsSender,
      this.uniqueIdProvider
yogevbd's avatar
yogevbd committed
31
    );
yogevbd's avatar
yogevbd committed
32
    this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver, this.completionCallbackWrapper);
yogevbd's avatar
yogevbd committed
33

34 35
    this.ios = new NotificationsIOS(this.commands);
    this.android = new NotificationsAndroid(this.commands);
yogevbd's avatar
yogevbd committed
36 37
  }

38 39 40 41 42 43 44 45
  /**
   * registerRemoteNotifications
   */
  public registerRemoteNotifications() {
    this.ios.registerRemoteNotifications();
    this.android.registerRemoteNotifications();
  }

yogevbd's avatar
yogevbd committed
46
  /**
yogevbd's avatar
yogevbd committed
47
   * postLocalNotification
yogevbd's avatar
yogevbd committed
48
   */
yogevbd's avatar
yogevbd committed
49 50
  public postLocalNotification(notification: Notification, id: number) {
    return this.commands.postLocalNotification(notification, id);
yogevbd's avatar
yogevbd committed
51 52 53
  }

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

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  /**
   * setCategories
   */
  public setCategories(categories: [NotificationCategory?]) {
    this.commands.setCategories(categories);
  }

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

  /**
   * isRegisteredForRemoteNotifications
   */
77 78
  public isRegisteredForRemoteNotifications(): Promise<boolean> {
    return this.commands.isRegisteredForRemoteNotifications();
79 80
  }

yogevbd's avatar
yogevbd committed
81 82 83 84 85 86 87
  /**
   * Obtain the events registry instance
   */
  public events(): EventsRegistry {
    return this.eventsRegistry;
  }
}