Make iOS notifications compatible with background fetch (#5969)

This commit is contained in:
Elias Nahum 2022-02-16 22:58:01 -03:00 committed by GitHub
parent f929bf9b3a
commit b0b4e55f21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 6 deletions

View file

@ -76,7 +76,7 @@ export function loadConfigAndLicense() {
};
}
export function loadFromPushNotification(notification, isInitialNotification) {
export function loadFromPushNotification(notification, isInitialNotification, skipChannelSwitch = false) {
return async (dispatch, getState) => {
const state = getState();
const {payload} = notification;
@ -108,12 +108,14 @@ export function loadFromPushNotification(notification, isInitialNotification) {
await Promise.all(loading);
}
dispatch(handleSelectTeamAndChannel(teamId, channelId));
dispatch(selectPost(''));
if (!skipChannelSwitch) {
dispatch(handleSelectTeamAndChannel(teamId, channelId));
dispatch(selectPost(''));
const {root_id: rootId} = notification.payload || {};
if (isCollapsedThreadsEnabled(state) && rootId) {
dispatch(selectPost(rootId));
const {root_id: rootId} = notification.payload || {};
if (isCollapsedThreadsEnabled(state) && rootId) {
dispatch(selectPost(rootId));
}
}
return {data: true};
};

View file

@ -173,6 +173,17 @@ class PushNotifications {
EphemeralStore.setStartFromNotification(true);
notification.userInteraction = true;
if (Platform.OS === 'ios') {
// when a notification is received on iOS, getInitialNotification, will return the notification
// as the app will initialized cause we are using background fetch,
// that does not necessarily mean that the app was opened cause of the notification was tapped.
// Here we are going to dettermine if the notification still exists in NotificationCenter to verify if
// the app was opened because of a tap or cause of the background fetch init
const delivered = await Notifications.ios.getDeliveredNotifications();
notification.userInteraction = delivered.find((d) => (d as unknown as NotificationWithAck).ack_id === notification?.payload?.ack_id) == null;
notification.foreground = false;
}
// getInitialNotification may run before the store is set
// that is why we run on an interval until the store is available
// once we handle the notification the interval is cleared.
@ -221,6 +232,8 @@ class PushNotifications {
}
}
}
} else if (!userInteraction && Platform.OS === 'ios') {
dispatch(loadFromPushNotification(notification, isInitialNotification, true));
}
break;
case NOTIFICATION_TYPE.SESSION:

View file

@ -6,7 +6,12 @@ interface NotificationUserInfo {
test?: boolean;
}
interface NotificationWithAck extends Notification {
ack_id: string;
}
interface NotificationData {
ack_id: string;
body?: string;
channel_id: string;
channel_name?: string;