Skip to content

Commit

Permalink
Simplify notification subscription (#1848)
Browse files Browse the repository at this point in the history
  • Loading branch information
borisyankov authored Jan 31, 2018
1 parent e6eefcf commit 6264309
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/boot/AppEventHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import React, { PureComponent } from 'react';
import { AppState, NetInfo, View, StyleSheet, Platform, NativeModules } from 'react-native';
import SafeArea from 'react-native-safe-area';
import Orientation from 'react-native-orientation';
import NotificationsIOS, { NotificationsAndroid } from 'react-native-notifications';

import type { Auth, Actions, ChildrenArray } from '../types';
import connectWithActions from '../connectWithActions';
import { getAuth, getUnreadByHuddlesMentionsAndPMs } from '../selectors';
import { handlePendingNotifications, tryInitialNotification } from '../utils/notifications';
import {
addNotificationListener,
removeNotificationListener,
handlePendingNotifications,
tryInitialNotification,
} from '../utils/notifications';

const componentStyles = StyleSheet.create({
wrapper: {
Expand Down Expand Up @@ -52,7 +56,7 @@ class AppEventHandlers extends PureComponent<Props> {
}
};

handleNotificationOpen = notification => {
handleNotificationOpen = (notification: Object) => {
const { actions } = this.props;
actions.saveInitialNotificationDetails(notification.getData());
setTimeout(() => handlePendingNotifications(notification), 600);
Expand All @@ -71,21 +75,15 @@ class AppEventHandlers extends PureComponent<Props> {
AppState.addEventListener('memoryWarning', this.handleMemoryWarning);
SafeArea.getSafeAreaInsetsForRootView().then(actions.initSafeAreaInsets);
Orientation.addOrientationListener(this.handleOrientationChange);
if (Platform.OS === 'ios') {
NotificationsIOS.addEventListener('notificationOpened', this.handleNotificationOpen);
} else {
NotificationsAndroid.setNotificationOpenedListener(this.handleNotificationOpen);
}
addNotificationListener(this.handleNotificationOpen);
}

componentWillUnmount() {
NetInfo.removeEventListener('connectionChange', this.handleConnectivityChange);
AppState.removeEventListener('change', this.handleAppStateChange);
AppState.removeEventListener('memoryWarning', this.handleMemoryWarning);
Orientation.removeOrientationListener(this.handleOrientationChange);
if (Platform.OS === 'ios') {
NotificationsIOS.removeEventListener('notificationOpened', this.handleNotificationOpen);
}
removeNotificationListener(this.handleNotificationOpen);
}

render() {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/notifications.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ const handleRegistrationUpdates = (auth: Auth, saveTokenPush: Actions.saveTokenP
});
};

export const addNotificationListener = (notificationHandler: (notification: Object) => void) => {
NotificationsAndroid.setNotificationOpenedListener(notificationHandler);
};

export const removeNotificationListener = (
notificationHandler: (notification: Object) => void,
) => {};

export const initializeNotifications = (auth: Auth, saveTokenPush: Actions.saveTokenPush) => {
handleRegistrationUpdates(auth, saveTokenPush);
};
Expand Down
8 changes: 8 additions & 0 deletions src/utils/notifications.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ const onPushRegistrationFailed = (error: string) => {
logErrorRemotely(new Error(error), 'register ios push token failed');
};

export const addNotificationListener = (notificationHandler: (notification: Object) => void) => {
NotificationsIOS.addEventListener('notificationOpened', notificationHandler);
};

export const removeNotificationListener = (notificationHandler: (notification: Object) => void) => {
NotificationsIOS.removeEventListener('notificationOpened', notificationHandler);
};

export const initializeNotifications = (auth: Auth, saveTokenPush: Actions.saveTokenPush) => {
NotificationsIOS.addEventListener('remoteNotificationsRegistered', deviceToken =>
onPushRegistered(auth, deviceToken, saveTokenPush),
Expand Down

0 comments on commit 6264309

Please sign in to comment.