UNPKG

@talkjs/react-native

Version:

Official TalkJS SDK for React Native

48 lines (44 loc) 1.94 kB
"use strict"; import { getToken, getMessaging, onTokenRefresh, onNotificationOpenedApp, getInitialNotification } from '@react-native-firebase/messaging'; import Notifee, { EventType } from '@notifee/react-native'; import PushNotificationIOS from '@react-native-community/push-notification-ios'; import { TokenHandler } from './Notification'; export class IOSNotificationHandler extends TokenHandler { async setup(_, iosSettings) { if (iosSettings?.useFirebase) { const messaging = getMessaging(); onTokenRefresh(messaging, token => this.setPushRegistrationId(token)); // This is triggered when the application is opened from a background state onNotificationOpenedApp(messaging, message => { this.notificationPressed(message, false); }); // The initial notification is not null when the application is opened from // a quit state. getInitialNotification(messaging).then(message => { if (message) { this.notificationPressed(message, false); } }); this.setPushRegistrationId(await getToken(messaging)); } else { PushNotificationIOS.addEventListener('register', token => { this.setPushRegistrationId(token); }); PushNotificationIOS.addEventListener('registrationError', error => { console.error('APNS registration error: ', error); }); PushNotificationIOS.addEventListener('notification', notification => { notification.finish(PushNotificationIOS.FetchResult.NoData); }); /* In iOS, the background Event handler is never called. If the app is in the background, it is openend and then the foreground Event handler is called. */ Notifee.onForegroundEvent(event => { if (event.type === EventType.PRESS) { this.notificationPressed(event, true); } }); } return this; } } //# sourceMappingURL=iOSNotification.js.map