NativeEventsReceiver.ts 1.94 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';
6
import { NotificationActionResponse } from '../interfaces/NotificationActionResponse';
yogevbd's avatar
yogevbd committed
7 8 9 10 11 12 13

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

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

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

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

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

32 33 34 35
  public registerRemoteNotificationOpened(callback: (notification: Notification, completion: () => void, actionResponse?: NotificationActionResponse) => void): EmitterSubscription {
    return this.emitter.addListener('notificationOpened', (response, completion) => {
      const action = response.action ? new NotificationActionResponse(response.action) : undefined
      callback(new Notification(response.notification), completion, action);
36
    });
yogevbd's avatar
yogevbd committed
37 38 39 40 41
  }

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