From 860d4c582fe8b5f3b0de2be71ec69bd8c0abd705 Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Fri, 1 Feb 2019 19:37:07 -0300 Subject: [PATCH] MM-13897 preserve iOS push notifications when opening the app and set the proper badge count (#2541) --- app/push_notifications/push_notifications.ios.js | 5 +++++ app/screens/channel/channel.js | 4 ++++ app/screens/channel/channel_nav_bar/channel_drawer_button.js | 5 ----- ios/Mattermost/AppDelegate.m | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/push_notifications/push_notifications.ios.js b/app/push_notifications/push_notifications.ios.js index 62aec806a..88aface45 100644 --- a/app/push_notifications/push_notifications.ios.js +++ b/app/push_notifications/push_notifications.ios.js @@ -152,6 +152,8 @@ class PushNotification { clearChannelNotifications(channelId) { NotificationsIOS.getDeliveredNotifications((notifications) => { const ids = []; + let badgeCount = notifications.length; + for (let i = 0; i < notifications.length; i++) { const notification = notifications[i]; @@ -161,8 +163,11 @@ class PushNotification { } if (ids.length) { + badgeCount -= ids.length; NotificationsIOS.removeDeliveredNotifications(ids); } + + this.setApplicationIconBadgeNumber(badgeCount); }); } } diff --git a/app/screens/channel/channel.js b/app/screens/channel/channel.js index 09196d527..24fe49862 100644 --- a/app/screens/channel/channel.js +++ b/app/screens/channel/channel.js @@ -86,6 +86,10 @@ export default class Channel extends PureComponent { } else { this.props.actions.selectDefaultTeam(); } + + if (this.props.currentChannelId) { + PushNotifications.clearChannelNotifications(this.props.currentChannelId); + } } componentDidMount() { diff --git a/app/screens/channel/channel_nav_bar/channel_drawer_button.js b/app/screens/channel/channel_nav_bar/channel_drawer_button.js index bcefd51b6..dbc1e221a 100644 --- a/app/screens/channel/channel_nav_bar/channel_drawer_button.js +++ b/app/screens/channel/channel_nav_bar/channel_drawer_button.js @@ -13,7 +13,6 @@ import { import Icon from 'react-native-vector-icons/Ionicons'; import Badge from 'app/components/badge'; -import PushNotifications from 'app/push_notifications'; import {getTheme} from 'mattermost-redux/selectors/entities/preferences'; import {preventDoubleTap} from 'app/utils/tap'; import {makeStyleSheetFromTheme} from 'app/utils/theme'; @@ -61,10 +60,6 @@ class ChannelDrawerButton extends PureComponent { EventEmitter.on('drawer_opacity', this.setOpacity); } - componentDidUpdate() { - PushNotifications.setApplicationIconBadgeNumber(this.props.mentionCount); - } - componentWillUnmount() { EventEmitter.off('drawer_opacity', this.setOpacity); } diff --git a/ios/Mattermost/AppDelegate.m b/ios/Mattermost/AppDelegate.m index c1119b12e..c6295db82 100644 --- a/ios/Mattermost/AppDelegate.m +++ b/ios/Mattermost/AppDelegate.m @@ -115,7 +115,7 @@ NSString* const NotificationClearAction = @"clear"; [self cleanNotificationsFromChannel:channelId andUpdateBadge:NO]; } else if (state == UIApplicationStateInactive) { // When the notification is opened - [self cleanNotificationsFromChannel:channelId andUpdateBadge:YES]; + [self cleanNotificationsFromChannel:channelId andUpdateBadge:NO]; } [RNNotifications didReceiveRemoteNotification:userInfo];