NativeCommandsSender.ts 3.46 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
  requestPermissions(): void;
  abandonPermissions(): void;
12
  refreshToken(): void;
13 14 15 16 17 18 19 20 21
  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
22
  getDeliveredNotifications(): Array<Notification>;
23
  setCategories(categories: [NotificationCategory?]): void;
yogevbd's avatar
yogevbd committed
24 25
  finishPresentingNotification(notificationId: string, callback: NotificationCompletion): void;
  finishHandlingAction(notificationId: string): void;
yogevbd's avatar
yogevbd committed
26 27 28 29 30 31 32 33
}

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

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

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

  abandonPermissions() {
    return this.nativeCommandsModule.abandonPermissions();
  }
49

50 51 52 53
  refreshToken() {
    return this.nativeCommandsModule.refreshToken();
  }

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
  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
93

yogevbd's avatar
yogevbd committed
94 95 96 97
  public getDeliveredNotifications(): Array<Notification> {
    return this.nativeCommandsModule.getDeliveredNotifications();
  }

yogevbd's avatar
yogevbd committed
98 99 100 101 102 103 104
  finishPresentingNotification(notificationId: string, notificationCompletion: NotificationCompletion): void {
    this.nativeCommandsModule.finishPresentingNotification(notificationId, notificationCompletion);
  }

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