From 62643099bcf88bda91b3613d74d761011da1b3b4 Mon Sep 17 00:00:00 2001 From: Boris Yankov Date: Wed, 31 Jan 2018 19:51:32 +0200 Subject: [PATCH] Simplify notification subscription (#1848) --- src/boot/AppEventHandlers.js | 20 +++++++++----------- src/utils/notifications.android.js | 8 ++++++++ src/utils/notifications.ios.js | 8 ++++++++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/boot/AppEventHandlers.js b/src/boot/AppEventHandlers.js index 9292a503a70..c44f853ca71 100644 --- a/src/boot/AppEventHandlers.js +++ b/src/boot/AppEventHandlers.js @@ -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: { @@ -52,7 +56,7 @@ class AppEventHandlers extends PureComponent { } }; - handleNotificationOpen = notification => { + handleNotificationOpen = (notification: Object) => { const { actions } = this.props; actions.saveInitialNotificationDetails(notification.getData()); setTimeout(() => handlePendingNotifications(notification), 600); @@ -71,11 +75,7 @@ class AppEventHandlers extends PureComponent { 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() { @@ -83,9 +83,7 @@ class AppEventHandlers extends PureComponent { 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() { diff --git a/src/utils/notifications.android.js b/src/utils/notifications.android.js index ebdd3def0b5..faa21675a4d 100644 --- a/src/utils/notifications.android.js +++ b/src/utils/notifications.android.js @@ -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); }; diff --git a/src/utils/notifications.ios.js b/src/utils/notifications.ios.js index 6a652d411ab..5c072990a46 100644 --- a/src/utils/notifications.ios.js +++ b/src/utils/notifications.ios.js @@ -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),