Skip to content

Commit f35fe49

Browse files
committed
Merge branch 'mkirk/background-fetch'
2 parents a1de99f + 8dfc584 commit f35fe49

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

Signal/Signal-Info.plist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<key>UIBackgroundModes</key>
109109
<array>
110110
<string>audio</string>
111+
<string>fetch</string>
111112
<string>remote-notification</string>
112113
<string>voip</string>
113114
</array>

Signal/src/AppDelegate.m

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#import "SignalsNavigationController.h"
2121
#import "ViewControllerUtils.h"
2222
#import <AxolotlKit/SessionCipher.h>
23+
#import <PromiseKit/AnyPromise.h>
2324
#import <SignalMessaging/AppSetup.h>
2425
#import <SignalMessaging/Environment.h>
2526
#import <SignalMessaging/OWSContactsManager.h>
@@ -206,6 +207,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
206207
selector:@selector(registrationStateDidChange)
207208
name:RegistrationStateDidChangeNotification
208209
object:nil];
210+
[[NSNotificationCenter defaultCenter] addObserver:self
211+
selector:@selector(registrationLockDidChange:)
212+
name:NSNotificationName_2FAStateDidChange
213+
object:nil];
209214

210215
DDLogInfo(@"%@ application: didFinishLaunchingWithOptions completed.", self.logTag);
211216

@@ -594,6 +599,20 @@ - (void)applicationDidBecomeActive:(UIApplication *)application {
594599
DDLogInfo(@"%@ applicationDidBecomeActive completed.", self.logTag);
595600
}
596601

602+
- (void)enableBackgroundRefreshIfNecessary
603+
{
604+
[AppReadiness runNowOrWhenAppIsReady:^{
605+
if (OWS2FAManager.sharedManager.is2FAEnabled && [TSAccountManager isRegistered]) {
606+
// Ping server once a day to keep-alive 2FA clients.
607+
const NSTimeInterval kBackgroundRefreshInterval = 24 * 60 * 60;
608+
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:kBackgroundRefreshInterval];
609+
} else {
610+
[[UIApplication sharedApplication]
611+
setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalNever];
612+
}
613+
}];
614+
}
615+
597616
- (void)handleActivation
598617
{
599618
OWSAssertIsOnMainThread();
@@ -619,6 +638,8 @@ - (void)handleActivation
619638
// and continue cleaning in the background.
620639
[[OWSDisappearingMessagesJob sharedJob] startIfNecessary];
621640

641+
[self enableBackgroundRefreshIfNecessary];
642+
622643
// Mark all "attempting out" messages as "unsent", i.e. any messages that were not successfully
623644
// sent before the app exited should be marked as failures.
624645
[[[OWSFailedMessagesJob alloc] initWithPrimaryStorage:[OWSPrimaryStorage sharedManager]] run];
@@ -989,7 +1010,7 @@ - (void)application:(UIApplication *)application didReceiveLocalNotification:(UI
9891010
- (void)application:(UIApplication *)application
9901011
handleActionWithIdentifier:(NSString *)identifier
9911012
forLocalNotification:(UILocalNotification *)notification
992-
completionHandler:(void (^)())completionHandler
1013+
completionHandler:(void (^)(void))completionHandler
9931014
{
9941015
OWSAssertIsOnMainThread();
9951016

@@ -1016,7 +1037,7 @@ - (void)application:(UIApplication *)application
10161037
handleActionWithIdentifier:(NSString *)identifier
10171038
forLocalNotification:(UILocalNotification *)notification
10181039
withResponseInfo:(NSDictionary *)responseInfo
1019-
completionHandler:(void (^)())completionHandler
1040+
completionHandler:(void (^)(void))completionHandler
10201041
{
10211042
OWSAssertIsOnMainThread();
10221043

@@ -1040,6 +1061,28 @@ - (void)application:(UIApplication *)application
10401061
}];
10411062
}
10421063

1064+
- (void)application:(UIApplication *)application
1065+
performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
1066+
{
1067+
DDLogInfo(@"%@ performing background fetch", self.logTag);
1068+
[AppReadiness runNowOrWhenAppIsReady:^{
1069+
__block AnyPromise *job = [[SignalApp sharedApp].messageFetcherJob run].then(^{
1070+
// HACK: Call completion handler after n seconds.
1071+
//
1072+
// We don't currently have a convenient API to know when message fetching is *done* when
1073+
// working with the websocket.
1074+
//
1075+
// We *could* substantially rewrite the TSSocketManager to take advantage of the `empty` message
1076+
// But once our REST endpoint is fixed to properly de-enqueue fallback notifications, we can easily
1077+
// use the rest endpoint here rather than the websocket and circumvent making changes to critical code.
1078+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
1079+
completionHandler(UIBackgroundFetchResultNewData);
1080+
job = nil;
1081+
});
1082+
});
1083+
}];
1084+
}
1085+
10431086
- (void)versionMigrationsDidComplete
10441087
{
10451088
OWSAssertIsOnMainThread();
@@ -1136,6 +1179,8 @@ - (void)registrationStateDidChange
11361179

11371180
DDLogInfo(@"registrationStateDidChange");
11381181

1182+
[self enableBackgroundRefreshIfNecessary];
1183+
11391184
if ([TSAccountManager isRegistered]) {
11401185
DDLogInfo(@"localNumber: %@", [TSAccountManager localNumber]);
11411186

@@ -1153,6 +1198,11 @@ - (void)registrationStateDidChange
11531198
}
11541199
}
11551200

1201+
- (void)registrationLockDidChange:(NSNotification *)notification
1202+
{
1203+
[self enableBackgroundRefreshIfNecessary];
1204+
}
1205+
11561206
- (void)ensureRootViewController
11571207
{
11581208
OWSAssertIsOnMainThread();

0 commit comments

Comments
 (0)