diff --git a/app/actions/views/root.js b/app/actions/views/root.js index 0480b91c5..c2141a5ff 100644 --- a/app/actions/views/root.js +++ b/app/actions/views/root.js @@ -13,7 +13,6 @@ import {getMyTeams, getMyTeamMembers} from '@mm-redux/actions/teams'; import {Client4} from '@mm-redux/client'; import {General} from '@mm-redux/constants'; import EventEmitter from '@mm-redux/utils/event_emitter'; -import EphemeralStore from '@store/ephemeral_store'; import initialState from '@store/initial_state'; import {getStateForReset} from '@store/utils'; @@ -133,9 +132,8 @@ export function handleSelectTeamAndChannel(teamId, channelId) { dispatch(batchActions(actions, 'BATCH_SELECT_TEAM_AND_CHANNEL')); } - EphemeralStore.setStartFromNotification(false); - - console.log('channel switch from push notification to', channel?.display_name, (Date.now() - dt), 'ms'); //eslint-disable-line + // eslint-disable-next-line no-console + console.log('channel switch from push notification to', channel?.display_name || channel?.id, (Date.now() - dt), 'ms'); }; } diff --git a/app/init/push_notifications.js b/app/init/push_notifications.js index f98efacdd..d246f0585 100644 --- a/app/init/push_notifications.js +++ b/app/init/push_notifications.js @@ -39,8 +39,8 @@ class PushNotificationUtils { this.replyNotificationData = null; } - configure = () => { - PushNotifications.configure({ + configure = async () => { + return PushNotifications.configure({ onRegister: this.onRegisterDevice, onNotification: this.onPushNotification, onReply: this.onPushNotificationReply, @@ -50,8 +50,6 @@ class PushNotificationUtils { }; loadFromNotification = async (notification) => { - // Set appStartedFromPushNotification to avoid channel screen to call selectInitialChannel - EphemeralStore.setStartFromNotification(true); await Store.redux.dispatch(loadFromPushNotification(notification)); // if we have a componentId means that the app is already initialized diff --git a/app/mattermost.js b/app/mattermost.js index 22f5f23cb..8d2799fae 100644 --- a/app/mattermost.js +++ b/app/mattermost.js @@ -29,15 +29,15 @@ import {captureJSException} from '@utils/sentry'; const init = async () => { const credentials = await getAppCredentials(); - const MMKVStorage = await getStorage(); - - const {store} = configureStore(MMKVStorage); if (EphemeralStore.appStarted) { launchApp(credentials); return; } - pushNotifications.configure(); + const MMKVStorage = await getStorage(); + const {store} = configureStore(MMKVStorage); + + await pushNotifications.configure(); globalEventHandler.configure({ launchApp, }); @@ -63,6 +63,8 @@ const launchApp = (credentials) => { if (valid) { store.dispatch(loadMe()); await globalEventHandler.configureAnalytics(); + // eslint-disable-next-line no-console + console.log('Launch app in Channel screen'); resetToChannel({skipMetrics: true}); } else { const error = new Error(`Previous app version "${previousVersion}" is invalid.`); diff --git a/app/push_notifications/push_notifications.android.js b/app/push_notifications/push_notifications.android.js index ac8109622..66e787052 100644 --- a/app/push_notifications/push_notifications.android.js +++ b/app/push_notifications/push_notifications.android.js @@ -31,6 +31,7 @@ class PushNotification { NotificationsAndroid.setNotificationOpenedListener((notification) => { if (notification) { + EphemeralStore.setStartFromNotification(true); const data = notification.getData(); this.handleNotification(data, true); } @@ -38,15 +39,16 @@ class PushNotification { } handleNotification = (data, userInteraction) => { + const foreground = !userInteraction && AppState.currentState === 'active'; this.deviceNotification = { data, - foreground: !userInteraction && AppState.currentState === 'active', + foreground, message: data.message, userInfo: data.userInfo, userInteraction, }; - if (this.onNotification) { + if (this.onNotification && (foreground || userInteraction)) { this.onNotification(this.deviceNotification); } }; @@ -59,21 +61,29 @@ class PushNotification { this.onRegister({token: this.deviceToken}); } - if (options.popInitialNotification) { + return new Promise((resolve) => { + if (!options.popInitialNotification) { + resolve(); + return; + } + PendingNotifications.getInitialNotification(). then((notification) => { if (notification) { const data = notification.getData(); if (data) { - EphemeralStore.appStartedFromPushNotification = true; + EphemeralStore.setStartFromNotification(true); this.handleNotification(data, true); } } }). catch((err) => { console.log('Android getInitialNotifiation() failed', err); //eslint-disable-line no-console + }). + finally(() => { + resolve(); }); - } + }); } localNotificationSchedule(notification) { diff --git a/app/push_notifications/push_notifications.ios.js b/app/push_notifications/push_notifications.ios.js index 86388c5db..9ef41b29c 100644 --- a/app/push_notifications/push_notifications.ios.js +++ b/app/push_notifications/push_notifications.ios.js @@ -51,7 +51,12 @@ class PushNotification { this.requestNotificationReplyPermissions(); - if (options.popInitialNotification) { + return new Promise((resolve) => { + if (!options.popInitialNotification) { + resolve(); + return; + } + NotificationsIOS.getInitialNotification(). then((notification) => { if (notification) { @@ -64,8 +69,11 @@ class PushNotification { }). catch((err) => { console.log('iOS getInitialNotifiation() failed', err); //eslint-disable-line no-console + }). + finally(() => { + resolve(); }); - } + }); } requestNotificationReplyPermissions = () => { diff --git a/app/screens/channel/channel_base.js b/app/screens/channel/channel_base.js index a6e8e8210..f76f20ab6 100644 --- a/app/screens/channel/channel_base.js +++ b/app/screens/channel/channel_base.js @@ -184,7 +184,11 @@ export default class ChannelBase extends PureComponent { loadChannels = (teamId) => { const {loadChannelsForTeam, selectInitialChannel} = this.props.actions; - if (!EphemeralStore.getStartFromNotification()) { + if (EphemeralStore.getStartFromNotification()) { + // eslint-disable-next-line no-console + console.log('Switch to channel from a push notification'); + EphemeralStore.setStartFromNotification(false); + } else { loadChannelsForTeam(teamId).then((result) => { if (result?.error) { this.setState({channelsRequestFailed: true});