Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async componentDidMount() {
// Add an event listener to log registration errors in development
if (__DEV__) {
PushNotificationIOS.addEventListener('registrationError', error =>
console.warn(error)
)
}
PushNotification.configure({
// Called when Token is generated (iOS and Android) (optional)
onRegister: deviceToken => {
// Save the device token into redux for later use with other accounts
this.props.setDeviceToken(deviceToken['token'])
},
// Called when a remote or local notification is opened or received
onNotification: notification => {
this.onNotification(notification)
// https://facebook.github.io/react-native/docs/pushnotificationios.html
if (Platform.OS === 'ios') {
notification.finish(PushNotificationIOS.FetchResult.NoData)
// completely closed
PushNotificationIOS.getInitialNotification().then(notification => {
if (notification) {
// backgroundNotification is an instance of PushNotificationIOS, create
// a notification object from it
const notificationObj = {
alert: this.state.backgroundNotification.getAlert(),
data: this.state.backgroundNotification.getData()
}
// Pop the alert with option to redirect to WebView
this.onNotification(notificationObj)
}
})
// Get notifications that were triggered when the app was backgrounded
PushNotificationIOS.addEventListener('notification', notification => {
if (AppState.currentState === 'background') {
// Save notification to state so it can be dealt with when the user
// foregrounds the app
console.debug('Setting background notification')
this.setState({ backgroundNotification: notification })
}
})
AppState.addEventListener('change', newState => {
if (
newState === 'active' &&
this.state.backgroundNotification !== null
) {
// backgroundNotification is an instance of PushNotificationIOS, create
// a notification object from it
const notification = {
addEventListenerForIOS(event, handler) {
const that = this;
if (event === REMOTE_TOKEN_RECEIVED) {
PushNotificationIOS.addEventListener('register', data => {
handler(data);
});
}
if (event === REMOTE_NOTIFICATION_RECEIVED) {
PushNotificationIOS.addEventListener('notification', handler);
}
}
addEventListenerForIOS(event, handler) {
const that = this;
if (event === REMOTE_TOKEN_RECEIVED) {
PushNotificationIOS.addEventListener('register', data => {
handler(data);
});
}
if (event === REMOTE_NOTIFICATION_RECEIVED) {
PushNotificationIOS.addEventListener('notification', handler);
}
}