NativeEventsReceiver.ts 1.69 KB
Newer Older
yogevbd's avatar
yogevbd committed
1 2
import { NativeModules, NativeEventEmitter, EventEmitter, EmitterSubscription } from 'react-native';
import {
yogevbd's avatar
yogevbd committed
3
  Registered, RegistrationError, RegisteredPushKit
yogevbd's avatar
yogevbd committed
4
} from '../interfaces/NotificationEvents';
5
import { Notification } from '../DTO/Notification';
yogevbd's avatar
yogevbd committed
6 7 8 9 10 11 12

export class NativeEventsReceiver {
  private emitter: EventEmitter;
  constructor() {
    this.emitter = new NativeEventEmitter(NativeModules.RNEventEmitter);
  }

yogevbd's avatar
yogevbd committed
13
  public registerRemoteNotificationsRegistered(callback: (event: Registered) => void): EmitterSubscription {
yogevbd's avatar
yogevbd committed
14 15 16
    return this.emitter.addListener('remoteNotificationsRegistered', callback);
  }

yogevbd's avatar
yogevbd committed
17 18 19 20 21
  public registerPushKitRegistered(callback: (event: RegisteredPushKit) => void): EmitterSubscription {
    return this.emitter.addListener('pushKitRegistered', callback);
  }

  public registerRemoteNotificationReceived(callback: (notification: Notification) => void): EmitterSubscription {
22 23 24
    return this.emitter.addListener('notificationReceived', (payload) => {
      callback(new Notification(payload));
    });
yogevbd's avatar
yogevbd committed
25 26 27
  }

  public registerPushKitNotificationReceived(callback: (event: object) => void): EmitterSubscription {
yogevbd's avatar
yogevbd committed
28
    return this.emitter.addListener('pushKitNotificationReceived', callback);
yogevbd's avatar
yogevbd committed
29
  }
yogevbd's avatar
yogevbd committed
30

yogevbd's avatar
yogevbd committed
31
  public registerRemoteNotificationOpened(callback: (response: Notification, completion: () => void) => void): EmitterSubscription {
32 33 34
    return this.emitter.addListener('notificationOpened', (payload, completion) => {
      callback(new Notification(payload), completion);
    });
yogevbd's avatar
yogevbd committed
35 36 37 38 39
  }

  public registerRemoteNotificationsRegistrationFailed(callback: (event: RegistrationError) => void): EmitterSubscription {
    return this.emitter.addListener('remoteNotificationsRegistrationFailed', callback);
  }
yogevbd's avatar
yogevbd committed
40
}