@talkjs/expo
Version:
Official TalkJS SDK for React Native (Expo)
58 lines (54 loc) • 2.44 kB
JavaScript
;
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));
// This is triggered when the application is opened from a background state
FirebaseMessaging.onNotificationOpenedApp(messaging, message => {
this.notificationPressed(message, false);
});
// The initial notification is not null when the application is opened from
// a quit state.
FirebaseMessaging.getInitialNotification(messaging).then(message => {
if (message) {
this.notificationPressed(message, false);
}
});
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 token = await ExpoNotifications.getDevicePushTokenAsync();
this.setPushRegistrationId(token.data);
}
} catch (e) {
console.error('Could not setup Expo Notifications. Ensure expo-notifications is properly installed: ', e);
}
/* 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