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 = {
JSON.stringify(pushImageSummary),
)
await downloadAndUnzipIssue(pushImageSummary, screenSize)
await pushTracking('pushDownloadComplete', 'completed')
notificationTracking(notificationId, 'downloaded')
} catch (e) {
await pushTracking('pushDownloadError', JSON.stringify(e))
errorService.captureException(e)
} finally {
// No matter what happens, always clear up old issues
await clearOldIssues()
// required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
notification.finish(PushNotificationIOS.FetchResult.NoData)
}
}
},
senderID: defaultSettings.senderId,
onNotification: notification => {
this.onNotification(notification)
// https://facebook.github.io/react-native/docs/pushnotificationios.html
if (Platform.OS === 'ios') {
notification.finish(PushNotificationIOS.FetchResult.NoData)
}
},
// Android only
onNotification: notification => {
store.dispatch({type: NOTIFICATION_CLICKED})
// If we were able to set some data along with the notification (iOS), handle it!
const { data } = notification
if (data && data.conversationId)
Actions.conversation({conversationId: data.conversationId})
else if (data && data.bellId)
Actions.bells()
else
Actions.conversations()
// If we're on iOS, trigger the expected callback
if (Platform.OS === 'ios')
notification.finish(PushNotificationIOS.FetchResult.NoData)
}
})
const onNotification = (notification: PushNotificationIOS) => {
if (!notification.foreground) {
// @ts-ignore
this.navigator.dispatch(
NavigationActions.navigate({
routeName: routes.accountDetails,
params: {
accountRS: notification.data.accountRS
}
})
);
}
// required on iOS only (see fetchCompletionHandler docs:
// https://github.com/react-native-community/react-native-push-notification-ios)
notification.finish(PushNotificationIOS.FetchResult.NoData);
};
notification.finish(PushNotificationIOS.FetchResult.NoData)
}
},
// Android only
senderID: '162663374736',
// iOS only
permissions: DEFAULT_NOTIFICATION_PERMISSIONS,
// Should the initial notification be popped automatically
popInitialNotification: true,
requestPermissions: Platform.OS !== 'ios'
})
if (Platform.os === 'ios') {
// Get notifications that triggered an open of the app when the app was
// 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
_checkIfOpenedByCampaign(nextAppState) {
// the app is turned from background to foreground
if (
this._currentState.match(/inactive|background/) &&
nextAppState === 'active'
) {
PushNotificationIOS.getInitialNotification()
.then(data => {
if (data) {
this.handleCampaignOpened(data);
}
})
.catch(e => {
logger.debug('Failed to get the initial notification.', e);
});
}
this._currentState = nextAppState;
}
if (action.payload.type === MessageType.RECEIVED && scene === 'conversation' && sceneId == action.payload.cid)
yield put({type: MESSAGE_READ, payload: action.payload.cid})
}
// Get and count the number of unread conversations and bells
const conversations = yield select(state => state.conversations)
, unreadConversations = countUnread(conversations)
, bells = yield select(state => state.bells)
, unreadBells = bells.reduce((sum, bell: Bell) => sum + (bell.isRead ? 0 : 1), 0)
, unread = unreadConversations + unreadBells
// Set the badge count depending on the relevant bridge
yield call(
Platform.OS === 'ios' ?
PushNotificationIOS.setApplicationIconBadgeNumber :
AndroidBadge.setBadge,
unread
)
}
}
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);
}
}