NativeCommandsSender.ts 3.36 KB
Newer Older
yogevbd's avatar
yogevbd committed
1
import { NativeModules } from 'react-native';
2
import { Notification } from '../DTO/Notification';
3 4 5
import { NotificationCompletion } from '../interfaces/NotificationCompletion';
import { NotificationPermissions } from '../interfaces/NotificationPermissions';
import { NotificationCategory } from '../interfaces/NotificationCategory';
yogevbd's avatar
yogevbd committed
6 7

interface NativeCommandsModule {
8
  getInitialNotification(): Promise<Notification>;
yogevbd's avatar
yogevbd committed
9
  postLocalNotification(notification: Notification, id: number): void;
10 11 12 13 14 15 16 17 18 19 20
  requestPermissions(): void;
  abandonPermissions(): void;
  registerPushKit(): void;
  getBadgeCount(): Promise<number>;
  setBadgeCount(count: number): void;
  cancelLocalNotification(notificationId: string): void;
  cancelAllLocalNotifications(): void;
  isRegisteredForRemoteNotifications(): Promise<boolean>;
  checkPermissions(): Promise<NotificationPermissions>;
  removeDeliveredNotifications(identifiers: Array<string>): void;
  removeAllDeliveredNotifications(): void;
yogevbd's avatar
yogevbd committed
21
  getDeliveredNotifications(): Array<Notification>;
22
  setCategories(categories: [NotificationCategory?]): void;
yogevbd's avatar
yogevbd committed
23 24
  finishPresentingNotification(notificationId: string, callback: NotificationCompletion): void;
  finishHandlingAction(notificationId: string): void;
yogevbd's avatar
yogevbd committed
25 26 27 28 29 30 31 32
}

export class NativeCommandsSender {
  private readonly nativeCommandsModule: NativeCommandsModule;
  constructor() {
    this.nativeCommandsModule = NativeModules.RNBridgeModule;
  }

yogevbd's avatar
yogevbd committed
33 34
  postLocalNotification(notification: Notification, id: number) {
    return this.nativeCommandsModule.postLocalNotification(notification, id);
yogevbd's avatar
yogevbd committed
35 36
  }

yogevbd's avatar
yogevbd committed
37
  getInitialNotification(): Promise<Notification> {
yogevbd's avatar
yogevbd committed
38 39 40 41
    return this.nativeCommandsModule.getInitialNotification();
  }
  
  requestPermissions() {
42
    return this.nativeCommandsModule.requestPermissions();
yogevbd's avatar
yogevbd committed
43
  }
yogevbd's avatar
yogevbd committed
44 45 46 47

  abandonPermissions() {
    return this.nativeCommandsModule.abandonPermissions();
  }
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

  registerPushKit() {
    return this.nativeCommandsModule.registerPushKit();
  }

  setCategories(categories: [NotificationCategory?]) {
    this.nativeCommandsModule.setCategories(categories);
  }

  getBadgeCount(): Promise<number> {
    return this.nativeCommandsModule.getBadgeCount();
  }

  setBadgeCount(count: number) {
    this.nativeCommandsModule.setBadgeCount(count);
  }

  cancelLocalNotification(notificationId: string) {
    this.nativeCommandsModule.cancelLocalNotification(notificationId);
  }

  cancelAllLocalNotifications() {
    this.nativeCommandsModule.cancelAllLocalNotifications();
  }

  isRegisteredForRemoteNotifications(): Promise<any> {
    return this.nativeCommandsModule.isRegisteredForRemoteNotifications();
  }

  checkPermissions() {
    return this.nativeCommandsModule.checkPermissions();
  }

  removeAllDeliveredNotifications() {
    return this.nativeCommandsModule.removeAllDeliveredNotifications();
  }

  removeDeliveredNotifications(identifiers: Array<string>) {
    return this.nativeCommandsModule.removeDeliveredNotifications(identifiers);
  }
yogevbd's avatar
yogevbd committed
88

yogevbd's avatar
yogevbd committed
89 90 91 92
  public getDeliveredNotifications(): Array<Notification> {
    return this.nativeCommandsModule.getDeliveredNotifications();
  }

yogevbd's avatar
yogevbd committed
93 94 95 96 97 98 99
  finishPresentingNotification(notificationId: string, notificationCompletion: NotificationCompletion): void {
    this.nativeCommandsModule.finishPresentingNotification(notificationId, notificationCompletion);
  }

  finishHandlingAction(notificationId: string): void {
    this.nativeCommandsModule.finishHandlingAction(notificationId);
  }
yogevbd's avatar
yogevbd committed
100
}