Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
resolve();
} catch (err) {
reject(err);
}
}
NotificationsIOS.addEventListener('remoteNotificationsRegistered', onRegister);
async function onRegistrationError(e) {
NotificationsIOS.removeEventListener('remoteNotificationsRegistrationFailed', onRegistrationError);
log.info('PUSH: registration error', e);
reject(e);
}
NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', onRegistrationError);
// $FlowFixMe: error in type annotations of library
NotificationsIOS.requestPermissions();
});
}
// though I haven't successfully traced all the steps there:
// https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623022-application?language=objc
// which certainly in the wix case leads to a call to this:
// https://developer.apple.com/documentation/uikit/uiapplication/1623078-registerforremotenotifications
// which "initiate[s] the registration process with [APNs]". Then the
// methods that calls on success/failure in turn are implemented to send
// an event with name `remoteNotificationsRegistered` etc., in both the
// wix and RN-upstream case. (Though NB in the *failure* case, the
// event names differ! wix has s/Error/Failed/ vs. upstream; also
// upstream has singular for failure although plural for success, ouch.)
//
// In short, this kicks off a sequence: permissions -> "register" ->
// send event we already have a global listener for. And the first two
// steps satisfy the stern warnings in Apple's docs (at the above links)
// to request permissions first, then "register".
NotificationsIOS.requestPermissions();
} else {
// On Android, we do this at application startup.
}
};
export function initializePushNotifications() {
// $FlowFixMe: error in type annotations of library
NotificationsIOS.requestPermissions();
NotificationsIOS.consumeBackgroundQueue();
}
async requestPermissions() {
try {
NotificationsIOS.requestPermissions([this.alarmActions]);
await NativeModules.CMSiOSNotificationPermissionsManager.requestPermissions();
} catch (e) {
console.log(e);
}
}
}
});
NotificationsIOS.addEventListener('notificationOpened', (notification, completion) => {
const { background } = reduxStore.getState().app;
if (background) {
this.onNotification(notification);
}
completion();
});
const actions = [];
actions.push(new NotificationCategory({
identifier: 'MESSAGE',
actions: [replyAction]
}));
NotificationsIOS.requestPermissions(actions);
}
requestPermissions = (permissions) => {
NotificationsIOS.requestPermissions(permissions);
};
constructor(props) {
super(props);
switch (Platform.OS) {
case 'ios':
NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered);
NotificationsIOS.addEventListener(
'remoteNotificationsRegistrationFailed',
this.onPushRegistrationFailed,
);
if (!DeviceInfo.isEmulator()) {
NotificationsIOS.requestPermissions();
}
NotificationsIOS.addEventListener('notificationReceivedForeground', notification => {
this.onNotificationReceivedForeground(notification.getData());
});
NotificationsIOS.addEventListener('notificationReceivedBackground', notification => {
this.onNotificationReceivedBackground(notification.getData());
});
NotificationsIOS.addEventListener('notificationOpened', notification => {
this.onNotificationOpened(notification.getData());
});
break;
case 'android':
if (!LISTENERS_ADDED) {
LISTENERS_ADDED = true;
NotificationsAndroid.setRegistrationTokenUpdateListener(async deviceToken => {