From 1a170ba48458b25c1154222272ad5a2583d502ab Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 11 May 2018 11:47:31 -0400 Subject: [PATCH 1/4] Simplify debug notifications. --- Signal/src/AppDelegate.m | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 4bed6ee04..e14c6b6f7 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -533,11 +533,24 @@ static NSTimeInterval launchStartedAt; // When opening the app from a notification, // AppDelegate.didReceiveLocalNotification will always // be called _before_ we become active. - [application cancelAllLocalNotifications]; + [self cancelAllNotifications]; DDLogInfo(@"%@ applicationDidBecomeActive completed.", self.logTag); } +- (void)cancelAllNotifications +{ + DDLogInfo(@"%@ cancelAllNotifications.", self.logTag); + + // This will cancel all "scheduled" local notifications that haven't + // been presented yet. + [UIApplication.sharedApplication cancelAllLocalNotifications]; + // To clear all already presented local notifications, we need to + // set the app badge number to zero after setting it to a non-zero value. + [UIApplication sharedApplication].applicationIconBadgeNumber = 1; + [UIApplication sharedApplication].applicationIconBadgeNumber = 0; +} + - (void)enableBackgroundRefreshIfNecessary { [AppReadiness runNowOrWhenAppIsReady:^{ @@ -653,7 +666,7 @@ static NSTimeInterval launchStartedAt; DDLogWarn(@"%@ applicationWillResignActive.", self.logTag); // Clear all notifications whenever we become inactive. - [application cancelAllLocalNotifications]; + [self cancelAllNotifications]; [DDLog flushLog]; } From 9a4889c4fb4caf8c5b2568eaba62131ca13c6c87 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 11 May 2018 11:50:59 -0400 Subject: [PATCH 2/4] Simplify debug notifications. --- Signal/src/AppDelegate.m | 2 -- Signal/src/environment/SignalApp.m | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index e14c6b6f7..24ae5a20c 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -87,8 +87,6 @@ static NSTimeInterval launchStartedAt; - (void)applicationWillEnterForeground:(UIApplication *)application { DDLogWarn(@"%@ applicationWillEnterForeground.", self.logTag); - - [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; } - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application diff --git a/Signal/src/environment/SignalApp.m b/Signal/src/environment/SignalApp.m index 49198f316..f4304b5d1 100644 --- a/Signal/src/environment/SignalApp.m +++ b/Signal/src/environment/SignalApp.m @@ -227,6 +227,10 @@ [OWSStorage resetAllStorage]; [[OWSProfileManager sharedManager] resetProfileStorage]; [Environment.preferences clear]; + // Setting the app badge number from non-zero to zero has the + // side effect of clearing all scheduled and presented local + // notifications. + [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; [DebugLogger.sharedLogger wipeLogs]; From 9936003637f52a42260ebb4da6652d9c12926b62 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 11 May 2018 15:37:13 -0400 Subject: [PATCH 3/4] Respond to CR. --- Signal/src/AppDelegate.m | 19 +++---------------- Signal/src/environment/SignalApp.h | 2 ++ Signal/src/environment/SignalApp.m | 20 +++++++++++++++----- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 24ae5a20c..9e4da121e 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -531,24 +531,11 @@ static NSTimeInterval launchStartedAt; // When opening the app from a notification, // AppDelegate.didReceiveLocalNotification will always // be called _before_ we become active. - [self cancelAllNotifications]; + [SignalApp clearAllNotifications]; DDLogInfo(@"%@ applicationDidBecomeActive completed.", self.logTag); } -- (void)cancelAllNotifications -{ - DDLogInfo(@"%@ cancelAllNotifications.", self.logTag); - - // This will cancel all "scheduled" local notifications that haven't - // been presented yet. - [UIApplication.sharedApplication cancelAllLocalNotifications]; - // To clear all already presented local notifications, we need to - // set the app badge number to zero after setting it to a non-zero value. - [UIApplication sharedApplication].applicationIconBadgeNumber = 1; - [UIApplication sharedApplication].applicationIconBadgeNumber = 0; -} - - (void)enableBackgroundRefreshIfNecessary { [AppReadiness runNowOrWhenAppIsReady:^{ @@ -602,7 +589,7 @@ static NSTimeInterval launchStartedAt; DDLogInfo(@"%@ running post launch block for unregistered user.", self.logTag); // Unregistered user should have no unread messages. e.g. if you delete your account. - [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; + [SignalApp clearAllNotifications]; [TSSocketManager requestSocketOpen]; @@ -664,7 +651,7 @@ static NSTimeInterval launchStartedAt; DDLogWarn(@"%@ applicationWillResignActive.", self.logTag); // Clear all notifications whenever we become inactive. - [self cancelAllNotifications]; + [SignalApp clearAllNotifications]; [DDLog flushLog]; } diff --git a/Signal/src/environment/SignalApp.h b/Signal/src/environment/SignalApp.h index e3116c9e4..f09bece9a 100644 --- a/Signal/src/environment/SignalApp.h +++ b/Signal/src/environment/SignalApp.h @@ -44,4 +44,6 @@ + (void)resetAppData; ++ (void)clearAllNotifications; + @end diff --git a/Signal/src/environment/SignalApp.m b/Signal/src/environment/SignalApp.m index f4304b5d1..de9e7a952 100644 --- a/Signal/src/environment/SignalApp.m +++ b/Signal/src/environment/SignalApp.m @@ -227,14 +227,24 @@ [OWSStorage resetAllStorage]; [[OWSProfileManager sharedManager] resetProfileStorage]; [Environment.preferences clear]; - // Setting the app badge number from non-zero to zero has the - // side effect of clearing all scheduled and presented local - // notifications. - [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1]; - [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; + + [self clearAllNotifications]; [DebugLogger.sharedLogger wipeLogs]; exit(0); } ++ (void)clearAllNotifications +{ + DDLogInfo(@"%@ clearAllNotifications.", self.logTag); + + // This will cancel all "scheduled" local notifications that haven't + // been presented yet. + [UIApplication.sharedApplication cancelAllLocalNotifications]; + // To clear all already presented local notifications, we need to + // set the app badge number to zero after setting it to a non-zero value. + [UIApplication sharedApplication].applicationIconBadgeNumber = 1; + [UIApplication sharedApplication].applicationIconBadgeNumber = 0; +} + @end From 81a40909f4d9e6b322c456beaca398781935c27d Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 11 May 2018 15:45:38 -0400 Subject: [PATCH 4/4] Respond to CR. --- Signal/src/AppDelegate.m | 14 ++++++++++++-- SignalServiceKit/src/Messages/OWSMessageUtils.m | 1 - 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 9e4da121e..4f7a66363 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -531,7 +531,7 @@ static NSTimeInterval launchStartedAt; // When opening the app from a notification, // AppDelegate.didReceiveLocalNotification will always // be called _before_ we become active. - [SignalApp clearAllNotifications]; + [self clearAllNotificationsAndRestoreBadgeCount]; DDLogInfo(@"%@ applicationDidBecomeActive completed.", self.logTag); } @@ -651,11 +651,21 @@ static NSTimeInterval launchStartedAt; DDLogWarn(@"%@ applicationWillResignActive.", self.logTag); // Clear all notifications whenever we become inactive. - [SignalApp clearAllNotifications]; + [self clearAllNotificationsAndRestoreBadgeCount]; [DDLog flushLog]; } +- (void)clearAllNotificationsAndRestoreBadgeCount +{ + OWSAssertIsOnMainThread(); + + [SignalApp clearAllNotifications]; + [AppReadiness runNowOrWhenAppIsReady:^{ + [OWSMessageUtils.sharedManager updateApplicationBadgeCount]; + }]; +} + - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL succeeded))completionHandler { diff --git a/SignalServiceKit/src/Messages/OWSMessageUtils.m b/SignalServiceKit/src/Messages/OWSMessageUtils.m index 165e11f67..b6c631cac 100644 --- a/SignalServiceKit/src/Messages/OWSMessageUtils.m +++ b/SignalServiceKit/src/Messages/OWSMessageUtils.m @@ -95,7 +95,6 @@ NS_ASSUME_NONNULL_BEGIN [CurrentAppContext() setMainAppBadgeNumber:numberOfItems]; } - @end NS_ASSUME_NONNULL_END