From 03241128f6937468f736a003db3e3aa900fc511e Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 6 Nov 2017 12:37:15 -0500 Subject: [PATCH] Respond to CR. // FREEBIE --- Signal/src/AppDelegate.m | 6 +++--- Signal/src/environment/NotificationsManager.h | 8 +++++--- Signal/src/environment/NotificationsManager.m | 7 +++++++ Signal/src/network/PushManager.m | 13 ++++++++++++- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index dfecc470d..c229bda98 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -765,21 +765,21 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; } - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { + OWSAssert([NSThread isMainThread]); + if (!self.isEnvironmentSetup) { OWSFail( @"%@ ignoring %s because environment is not yet set up: %@.", self.tag, __PRETTY_FUNCTION__, notification); return; } if (application.applicationState != UIApplicationStateActive) { - OWSFail(@"%@ ignoring %s because app is not yet active: %@.", self.tag, __PRETTY_FUNCTION__, notification); + DDLogInfo(@"%@ ignoring %s because app is not yet active: %@.", self.tag, __PRETTY_FUNCTION__, notification); return; } DDLogInfo(@"%@ %s %@", self.tag, __PRETTY_FUNCTION__, notification); [AppStoreRating preventPromptAtNextTest]; [[PushManager sharedManager] application:application didReceiveLocalNotification:notification]; - // We only want to receive a single local notification per launch. - [application cancelAllLocalNotifications]; } - (void)application:(UIApplication *)application diff --git a/Signal/src/environment/NotificationsManager.h b/Signal/src/environment/NotificationsManager.h index b017f51b6..8f18d930a 100644 --- a/Signal/src/environment/NotificationsManager.h +++ b/Signal/src/environment/NotificationsManager.h @@ -7,14 +7,16 @@ NS_ASSUME_NONNULL_BEGIN -@class TSCall; -@class TSContactThread; @class OWSContactsManager; -@class SignalCall; @class OWSPreferences; +@class SignalCall; +@class TSCall; +@class TSContactThread; @interface NotificationsManager : NSObject +- (void)clearAllNotifications; + @end NS_ASSUME_NONNULL_END diff --git a/Signal/src/environment/NotificationsManager.m b/Signal/src/environment/NotificationsManager.m index e5315b0f8..ef8ef308f 100644 --- a/Signal/src/environment/NotificationsManager.m +++ b/Signal/src/environment/NotificationsManager.m @@ -422,6 +422,13 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc"; }); } +- (void)clearAllNotifications +{ + OWSAssert([NSThread isMainThread]); + + [self.currentNotifications removeAllObjects]; +} + #pragma mark - Logging + (NSString *)tag diff --git a/Signal/src/network/PushManager.m b/Signal/src/network/PushManager.m index 4b8bce455..8aa74e1f7 100644 --- a/Signal/src/network/PushManager.m +++ b/Signal/src/network/PushManager.m @@ -4,6 +4,7 @@ #import "PushManager.h" #import "AppDelegate.h" +#import "NotificationsManager.h" #import "OWSContactsManager.h" #import "Signal-Swift.h" #import "ThreadUtil.h" @@ -34,6 +35,7 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe @property (nonatomic, readonly) OWSMessageSender *messageSender; @property (nonatomic, readonly) OWSMessageFetcherJob *messageFetcherJob; @property (nonatomic, readonly) CallUIAdapter *callUIAdapter; +@property (nonatomic, readonly) NotificationsManager *notificationsManager; @end @@ -53,13 +55,15 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe return [self initWithMessageFetcherJob:[Environment getCurrent].messageFetcherJob storageManager:[TSStorageManager sharedManager] callUIAdapter:[Environment getCurrent].callService.callUIAdapter - messageSender:[Environment getCurrent].messageSender]; + messageSender:[Environment getCurrent].messageSender + notificationsManager:[Environment getCurrent].notificationsManager]; } - (instancetype)initWithMessageFetcherJob:(OWSMessageFetcherJob *)messageFetcherJob storageManager:(TSStorageManager *)storageManager callUIAdapter:(CallUIAdapter *)callUIAdapter messageSender:(OWSMessageSender *)messageSender + notificationsManager:(NotificationsManager *)notificationsManager { self = [super init]; if (!self) { @@ -72,6 +76,7 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe _callBackgroundTask = UIBackgroundTaskInvalid; // TODO: consolidate notification tracking with NotificationsManager, which also maintains a list of notifications. _currentNotifications = [NSMutableArray array]; + _notificationsManager = notificationsManager; OWSSingletonAssert(); @@ -129,6 +134,7 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { + OWSAssert([NSThread isMainThread]); DDLogInfo(@"%@ launched from local notification", self.tag); NSString *_Nullable threadId = notification.userInfo[Signal_Thread_UserInfo_Key]; @@ -138,6 +144,11 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe } else { OWSFail(@"%@ threadId was unexpectedly nil in %s", self.tag, __PRETTY_FUNCTION__); } + + // We only want to receive a single local notification per launch. + [application cancelAllLocalNotifications]; + [self.currentNotifications removeAllObjects]; + [self.notificationsManager clearAllNotifications]; } - (void)application:(UIApplication *)application