UNPKG

@talkjs/expo

Version:

Official TalkJS SDK for React Native (Expo)

58 lines (56 loc) 2.84 kB
"use strict"; import Notifee, { EventType } from '@notifee/react-native'; import { TokenHandler } from './Notification'; import { ExpoNotificationsContext, FirebaseMessagingContext } from './Imports'; export class IOSNotificationHandler extends TokenHandler { async setup(_, iosSettings) { if (iosSettings?.useFirebase) { try { const FirebaseMessaging = FirebaseMessagingContext('./index.js'); if (FirebaseMessaging) { const messaging = FirebaseMessaging.getMessaging(); FirebaseMessaging.onTokenRefresh(messaging, token => this.setPushRegistrationId(token)); const permission = await FirebaseMessaging.hasPermission(messaging); const authorizationStatus = FirebaseMessaging.FirebaseMessagingTypes.AuthorizationStatus; const hasPermission = permission === authorizationStatus.AUTHORIZED || permission === authorizationStatus.PROVISIONAL || permission === authorizationStatus.EPHEMERAL; if (!this.deviceToken.pushRegistrationId && hasPermission) { const token = await FirebaseMessaging.getToken(messaging); this.setPushRegistrationId(token); } } } catch (e) { console.error('Could not setup Firebase Messaging. Ensure @react-native-firebase/messaging is properly installed: ', e); } } else { try { const ExpoNotifications = ExpoNotificationsContext('./index.js'); if (ExpoNotifications) { if (!this.pushTokenListenerSubscription) { this.pushTokenListenerSubscription = ExpoNotifications.addPushTokenListener(token => this.setPushRegistrationId(token.data)); } const permission = (await ExpoNotifications.getPermissionsAsync()).ios?.status; const authorizationStatus = ExpoNotifications.IosAuthorizationStatus; const hasPermission = permission === authorizationStatus.AUTHORIZED || permission === authorizationStatus.PROVISIONAL || permission === authorizationStatus.EPHEMERAL; if (!this.deviceToken.pushRegistrationId && hasPermission) { const token = await ExpoNotifications.getDevicePushTokenAsync(); this.setPushRegistrationId(token.data); } } } catch (e) { console.error('Could not setup Expo Notifications. Ensure expo-notifications is properly installed: ', e); } } if (this.resolveTokenPromise) { this.resolveTokenPromise(); } /* 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