react-native-notifications-pfy
Version:
Advanced Push Notifications (Silent, interactive notifications) for iOS & Android + Schedule Module
44 lines (36 loc) • 2.14 kB
text/typescript
import { NativeModules, NativeEventEmitter, EventEmitter, EmitterSubscription } from 'react-native';
import {
Registered, RegistrationError, RegisteredPushKit
} from '../interfaces/NotificationEvents';
import { Notification } from '../DTO/Notification';
import { NotificationActionResponse } from '../interfaces/NotificationActionResponse';
import { NotificationFactory } from '../DTO/NotificationFactory';
export class NativeEventsReceiver {
private emitter: EventEmitter;
constructor(private readonly notificationFactory: NotificationFactory) {
this.emitter = new NativeEventEmitter(NativeModules.RNEventEmitter);
}
public registerRemoteNotificationsRegistered(callback: (event: Registered) => void): EmitterSubscription {
return this.emitter.addListener('remoteNotificationsRegistered', callback);
}
public registerPushKitRegistered(callback: (event: RegisteredPushKit) => void): EmitterSubscription {
return this.emitter.addListener('pushKitRegistered', callback);
}
public registerNotificationReceived(callback: (notification: Notification) => void): EmitterSubscription {
return this.emitter.addListener('notificationReceived', (payload) => {
callback(this.notificationFactory.fromPayload(payload));
});
}
public registerPushKitNotificationReceived(callback: (event: object) => void): EmitterSubscription {
return this.emitter.addListener('pushKitNotificationReceived', callback);
}
public registerNotificationOpened(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(this.notificationFactory.fromPayload(response.notification), completion, action);
});
}
public registerRemoteNotificationsRegistrationFailed(callback: (event: RegistrationError) => void): EmitterSubscription {
return this.emitter.addListener('remoteNotificationsRegistrationFailed', callback);
}
}