Skip to content

Commit

Permalink
ios notif: Consume "background queue" so notifs navigate from backgro…
Browse files Browse the repository at this point in the history
…und.

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:
  wix/react-native-notifications@800669120
presumably in response to:
  wix/react-native-notifications#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: zulip#3647
  • Loading branch information
Chris Bobbe authored and gnprice committed Mar 9, 2020
1 parent 32ce040 commit c0e2233
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/boot/AppEventHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -89,6 +93,9 @@ class AppEventHandlers extends PureComponent<Props> {
if (state === 'background' && Platform.OS === 'android') {
NativeModules.BadgeCountUpdaterModule.setBadgeCount(unreadCount);
}
if (state === 'active') {
notificationOnAppActive();
}
};

notificationListener = new NotificationListener(this.props.dispatch);
Expand Down
23 changes: 23 additions & 0 deletions src/notification/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit c0e2233

Please sign in to comment.