From 7860478d54ec66656725c950a66214d12b973bdc Mon Sep 17 00:00:00 2001 From: Chris Duarte Date: Mon, 15 Jan 2018 10:59:25 -0800 Subject: [PATCH] Fix notification replies for iOS (#1341) * Fix notification replies for iOS * Review feedback * Review feedback 2 --- app/actions/views/root.js | 11 ++----- app/mattermost.js | 30 +++++++++++-------- .../push_notifications.ios.js | 4 +++ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/app/actions/views/root.js b/app/actions/views/root.js index 43fbb0d24..d2b812585 100644 --- a/app/actions/views/root.js +++ b/app/actions/views/root.js @@ -81,7 +81,7 @@ export function purgeOfflineStore() { } export function createPost(post) { - return async (dispatch, getState) => { + return (dispatch, getState) => { const state = getState(); const currentUserId = state.entities.users.currentUserId; @@ -95,8 +95,7 @@ export function createPost(post) { update_at: timestamp }; - try { - const payload = Client4.createPost({...newPost, create_at: 0}); + return Client4.createPost({...newPost, create_at: 0}).then((payload) => { dispatch({ type: PostTypes.RECEIVED_POSTS, data: { @@ -107,11 +106,7 @@ export function createPost(post) { }, channelId: payload.channel_id }); - } catch (error) { - return {error}; - } - - return {data: true}; + }); }; } diff --git a/app/mattermost.js b/app/mattermost.js index 43867c528..822e355d6 100644 --- a/app/mattermost.js +++ b/app/mattermost.js @@ -401,11 +401,12 @@ export default class Mattermost { const isNotActive = AppState.currentState !== 'active'; const notification = PushNotifications.getNotification(); - if (notification) { + if (notification || this.replyNotificationData) { // If we have a notification means that the app was started cause of a reply // and the app was not sitting in the background nor opened - const {data, text, badge} = notification; - this.onPushNotificationReply(data, text, badge); + const notificationData = notification || this.replyNotificationData; + const {data, text, badge, completed} = notificationData; + this.onPushNotificationReply(data, text, badge, completed); PushNotifications.resetNotification(); } @@ -521,17 +522,22 @@ export default class Mattermost { Client4.setToken(state.entities.general.credentials.token); } - createPost(post)(dispatch, getState); - markChannelAsRead(data.channel_id)(dispatch, getState); + createPost(post)(dispatch, getState).then(() => { + markChannelAsRead(data.channel_id)(dispatch, getState); - if (badge >= 0) { - PushNotifications.setApplicationIconBadgeNumber(badge); - } - } + if (badge >= 0) { + PushNotifications.setApplicationIconBadgeNumber(badge); + } - if (completed) { - // You must call to completed(), otherwise the action will not be triggered - completed(); + this.replyNotificationData = null; + }).then(completed); + } else { + this.replyNotificationData = { + data, + text, + badge, + completed + }; } }; diff --git a/app/push_notifications/push_notifications.ios.js b/app/push_notifications/push_notifications.ios.js index 0c1a75195..6c21dcbbe 100644 --- a/app/push_notifications/push_notifications.ios.js +++ b/app/push_notifications/push_notifications.ios.js @@ -132,6 +132,10 @@ class PushNotification { getNotification() { return null; } + + resetNotification() { + this.deviceNotification = null; + } } export default new PushNotification();