From c0e2233e763f291fd69d7ca794966dfbb459f883 Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Mon, 2 Mar 2020 18:24:22 -0800 Subject: [PATCH] ios notif: Consume "background queue" so notifs navigate from background. This is a short-term fix that relies on a deprecated API. We should upgrade to a newer version of react-native-notifications or use `@react-native-community/push-notification-ios` instead. For docs on this as of the library version we're using (1.5.0 on NPM, which has no Git tag but corresponds to commit 882775fb5), see: https://github.com/wix/react-native-notifications/blob/882775fb5/docs/notificationsEvents.md The API was removed in release 2.0.6-shapshot-8 with commit: https://github.com/wix/react-native-notifications/commit/800669120 presumably in response to: https://github.com/wix/react-native-notifications/issues/339 Process an opened notification from the "background queue" so our handleNotificationOpen will run, and the app will navigate, for a notification opened while the app was in the background. Fixes: #3647 --- src/boot/AppEventHandlers.js | 9 ++++++++- src/notification/index.js | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/boot/AppEventHandlers.js b/src/boot/AppEventHandlers.js index 66f78a125c3..e525461f0ad 100644 --- a/src/boot/AppEventHandlers.js +++ b/src/boot/AppEventHandlers.js @@ -10,7 +10,11 @@ import type { Node as React$Node } from 'react'; import type { Dispatch, Orientation as OrientationT } from '../types'; import { connect } from '../react-redux'; import { getUnreadByHuddlesMentionsAndPMs } from '../selectors'; -import { handleInitialNotification, NotificationListener } from '../notification'; +import { + handleInitialNotification, + NotificationListener, + notificationOnAppActive, +} from '../notification'; import { appOnline, appOrientation, appState, initSafeAreaInsets } from '../actions'; import PresenceHeartbeat from '../presence/PresenceHeartbeat'; @@ -89,6 +93,9 @@ class AppEventHandlers extends PureComponent { if (state === 'background' && Platform.OS === 'android') { NativeModules.BadgeCountUpdaterModule.setBadgeCount(unreadCount); } + if (state === 'active') { + notificationOnAppActive(); + } }; notificationListener = new NotificationListener(this.props.dispatch); diff --git a/src/notification/index.js b/src/notification/index.js index 32e0259978c..3ed221f27b6 100644 --- a/src/notification/index.js +++ b/src/notification/index.js @@ -132,6 +132,29 @@ export const handleInitialNotification = async (dispatch: Dispatch) => { dispatch(narrowToNotification(data)); }; +export const notificationOnAppActive = () => { + if (Platform.OS === 'ios') { + try { + // Allow 'notificationOpened' events to be emitted when pressing + // a notification when the app is in the background. + // + // TODO: This API is deprecated in react-native-notifications release + // 2.0.6-snapshot.8; see #3647. + // + // We don't know the behavior if this is called before + // NotificationsIOS.requestPermissions(), so, catch any errors + // silently. Ray's investigation shows that it *shouldn't* + // throw, but may (https://github.com/zulip/zulip-mobile/pull/3947#discussion_r389192513). + NotificationsIOS.consumeBackgroundQueue(); + } catch (e) { + logging.warn(e, { + message: + 'Call to NotificationsIOS.consumeBackgroundQueue failed; pressed notification failed to navigate', + }); + } + } +}; + /** * From rn-notifications@1.5.0's RNNotifications.m. */