From 24f33b2f971f777827e1cf9d0bd9f4bafc3552ea Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Fri, 5 Jul 2019 18:55:08 -0400 Subject: [PATCH] Fix regression opening a channel from a push notification (#2952) --- app/actions/views/channel.js | 27 ++++-- .../push_notifications.android.js | 3 + .../push_notifications.ios.js | 82 +++++++++++-------- app/utils/push_notifications.js | 5 -- 4 files changed, 68 insertions(+), 49 deletions(-) diff --git a/app/actions/views/channel.js b/app/actions/views/channel.js index 687d1178d..ee1db2d48 100644 --- a/app/actions/views/channel.js +++ b/app/actions/views/channel.js @@ -378,7 +378,7 @@ export function handleSelectChannel(channelId, fromPushNotification = false) { dispatch(loadPostsIfNecessaryWithRetry(channelId)); } - dispatch(batchActions([ + const actions = [ selectChannel(channelId), setChannelDisplayName(channel.display_name), { @@ -391,19 +391,30 @@ export function handleSelectChannel(channelId, fromPushNotification = false) { teamId: currentTeamId, channelId, }, - { - type: ViewTypes.SELECT_CHANNEL_WITH_MEMBER, - data: channelId, - channel, - member, - }, - ])); + ]; let markPreviousChannelId; if (!fromPushNotification && !sameChannel) { markPreviousChannelId = currentChannelId; + actions.push({ + type: ViewTypes.SELECT_CHANNEL_WITH_MEMBER, + data: currentChannelId, + channel: getChannel(state, currentChannelId), + member: getMyChannelMember(state, currentChannelId), + }); } + if (!fromPushNotification) { + actions.push({ + type: ViewTypes.SELECT_CHANNEL_WITH_MEMBER, + data: channelId, + channel, + member, + }); + } + + dispatch(batchActions(actions)); + dispatch(markChannelViewedAndRead(channelId, markPreviousChannelId)); }; } diff --git a/app/push_notifications/push_notifications.android.js b/app/push_notifications/push_notifications.android.js index 2e3943a32..cacbb9aef 100644 --- a/app/push_notifications/push_notifications.android.js +++ b/app/push_notifications/push_notifications.android.js @@ -4,6 +4,8 @@ import {AppState, NativeModules} from 'react-native'; import {NotificationsAndroid, PendingNotifications} from 'react-native-notifications'; +import ephemeralStore from 'app/store/ephemeral_store'; + const {NotificationPreferences} = NativeModules; class PushNotification { @@ -65,6 +67,7 @@ class PushNotification { if (notification) { const data = notification.getData(); if (data) { + ephemeralStore.appStartedFromPushNotification = true; this.handleNotification(data, true); } } diff --git a/app/push_notifications/push_notifications.ios.js b/app/push_notifications/push_notifications.ios.js index 88aface45..4140ae746 100644 --- a/app/push_notifications/push_notifications.ios.js +++ b/app/push_notifications/push_notifications.ios.js @@ -4,6 +4,8 @@ import {AppState} from 'react-native'; import NotificationsIOS, {NotificationAction, NotificationCategory} from 'react-native-notifications'; +import ephemeralStore from 'app/store/ephemeral_store'; + const CATEGORY = 'CAN_REPLY'; const REPLY_ACTION = 'REPLY_ACTION'; @@ -17,29 +19,10 @@ class PushNotification { this.onNotification = null; this.onReply = null; - NotificationsIOS.addEventListener('notificationReceivedForeground', (notification) => { - const info = { - ...notification.getData(), - message: notification.getMessage(), - }; - this.handleNotification(info, true, false); - }); - - NotificationsIOS.addEventListener('notificationReceivedBackground', (notification) => { - const info = { - ...notification.getData(), - message: notification.getMessage(), - }; - this.handleNotification(info, false, false); - }); - - NotificationsIOS.addEventListener('notificationOpened', (notification) => { - const info = { - ...notification.getData(), - message: notification.getMessage(), - }; - this.handleNotification(info, false, true); - }); + NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onRemoteNotificationsRegistered); + NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground); + NotificationsIOS.addEventListener('notificationReceivedBackground', this.onNotificationReceivedBackground); + NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened); const replyAction = new NotificationAction({ activationMode: 'background', @@ -59,7 +42,7 @@ class PushNotification { handleNotification = (data, foreground, userInteraction) => { this.deviceNotification = { data, - foreground: foreground || (!userInteraction && AppState.currentState === 'active'), + foreground, message: data.message, userInfo: data.userInfo, userInteraction, @@ -90,19 +73,9 @@ class PushNotification { this.onNotification = options.onNotification; this.onReply = options.onReply; - NotificationsIOS.addEventListener('remoteNotificationsRegistered', (deviceToken) => { - if (this.onRegister) { - this.onRegister({token: deviceToken}); - } - }); + this.requestPermissions([replyCategory]); - if (options.requestPermissions) { - this.requestPermissions([replyCategory]); - } - - if (options.popInitialNotification) { - NotificationsIOS.consumeBackgroundQueue(); - } + NotificationsIOS.consumeBackgroundQueue(); } requestPermissions = (permissions) => { @@ -136,6 +109,43 @@ class PushNotification { NotificationsIOS.cancelAllLocalNotifications(); } + onNotificationReceivedBackground = (notification) => { + const userInteraction = AppState.currentState === 'active'; + + // mark the app as started as soon as possible + if (userInteraction) { + ephemeralStore.appStartedFromPushNotification = true; + } + + const info = { + ...notification.getData(), + message: notification.getMessage(), + }; + this.handleNotification(info, false, userInteraction); + }; + + onNotificationReceivedForeground = (notification) => { + const info = { + ...notification.getData(), + message: notification.getMessage(), + }; + this.handleNotification(info, true, false); + }; + + onNotificationOpened = (notification) => { + const info = { + ...notification.getData(), + message: notification.getMessage(), + }; + this.handleNotification(info, false, true); + }; + + onRemoteNotificationsRegistered = (deviceToken) => { + if (this.onRegister) { + this.onRegister({token: deviceToken}); + } + }; + setApplicationIconBadgeNumber(number) { const count = number < 0 ? 0 : number; NotificationsIOS.setBadgesCount(count); diff --git a/app/utils/push_notifications.js b/app/utils/push_notifications.js index 7418f1046..b23a5d528 100644 --- a/app/utils/push_notifications.js +++ b/app/utils/push_notifications.js @@ -55,11 +55,6 @@ class PushNotificationUtils { let unsubscribeFromStore = null; let stopLoadingNotification = false; - // mark the app as started as soon as possible - if (!ephemeralStore.appStarted) { - ephemeralStore.appStartedFromPushNotification = true; - } - const {data, foreground, message, userInfo, userInteraction} = deviceNotification; const notification = { data,