@talkjs/react-native
Version:
Official TalkJS SDK for React Native
46 lines (44 loc) • 2.02 kB
JavaScript
;
import { getToken, getMessaging, onTokenRefresh, hasPermission, FirebaseMessagingTypes } 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));
const permission = await hasPermission(messaging);
const authorizationStatus = FirebaseMessagingTypes.AuthorizationStatus;
const hasPushPermission = permission === authorizationStatus.AUTHORIZED || permission === authorizationStatus.PROVISIONAL || permission === authorizationStatus.EPHEMERAL;
if (!this.deviceToken.pushRegistrationId && hasPushPermission) {
this.setPushRegistrationId(await getToken(messaging));
}
if (this.resolveTokenPromise) {
this.resolveTokenPromise();
}
} else {
PushNotificationIOS.addEventListener('register', async token => {
this.setPushRegistrationId(token);
if (this.resolveTokenPromise) {
this.resolveTokenPromise();
}
});
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