From 4aef593d7caba4a2a0301f3cb4abf7f6a97033e0 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 10 May 2019 12:02:04 +1000 Subject: [PATCH] Also poll for messages when the app is in the foreground --- Signal.xcodeproj/project.pbxproj | 12 ++++++++++++ Signal/src/AppDelegate.m | 12 ++++-------- Signal/src/Loki/Poller.swift | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 Signal/src/Loki/Poller.swift diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index 24af981af..3eeae078a 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -557,6 +557,7 @@ B6FE7EB71ADD62FA00A6D22F /* PushKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B6FE7EB61ADD62FA00A6D22F /* PushKit.framework */; }; B821F2F82272CED3002C88C0 /* OnboardingAccountDetailsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F72272CED3002C88C0 /* OnboardingAccountDetailsViewController.swift */; }; B821F2FA2272CEEE002C88C0 /* OnboardingKeyPairViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F92272CEEE002C88C0 /* OnboardingKeyPairViewController.swift */; }; + B843951A228510FB000563FE /* Poller.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8439519228510FB000563FE /* Poller.swift */; }; B90418E6183E9DD40038554A /* DateUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = B90418E5183E9DD40038554A /* DateUtil.m */; }; B9EB5ABD1884C002007CBB57 /* MessageUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B9EB5ABC1884C002007CBB57 /* MessageUI.framework */; }; BFF3FB9730634F37D25903F4 /* Pods_Signal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D17BB5C25D615AB49813100C /* Pods_Signal.framework */; }; @@ -1341,6 +1342,7 @@ B6FE7EB61ADD62FA00A6D22F /* PushKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PushKit.framework; path = System/Library/Frameworks/PushKit.framework; sourceTree = SDKROOT; }; B821F2F72272CED3002C88C0 /* OnboardingAccountDetailsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingAccountDetailsViewController.swift; sourceTree = ""; }; B821F2F92272CEEE002C88C0 /* OnboardingKeyPairViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingKeyPairViewController.swift; sourceTree = ""; }; + B8439519228510FB000563FE /* Poller.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Poller.swift; path = Signal/src/Loki/Poller.swift; sourceTree = SOURCE_ROOT; }; B90418E4183E9DD40038554A /* DateUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DateUtil.h; sourceTree = ""; }; B90418E5183E9DD40038554A /* DateUtil.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DateUtil.m; sourceTree = ""; }; B97940251832BD2400BD66CB /* UIUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIUtil.h; sourceTree = ""; }; @@ -2367,6 +2369,7 @@ 76EB041118170B33006006FC /* environment */, 34C4E2542118957600BEA353 /* Generated */, 45D231751DC7E8C50034FA89 /* Jobs */, + B8439518228510E9000563FE /* Loki */, 457F3AC01D14A0F700C51351 /* Models */, 76EB041D18170B33006006FC /* network */, 45B201741DAECBFD00C461E0 /* Signal-Bridging-Header.h */, @@ -2593,6 +2596,14 @@ name = Translations; sourceTree = ""; }; + B8439518228510E9000563FE /* Loki */ = { + isa = PBXGroup; + children = ( + B8439519228510FB000563FE /* Poller.swift */, + ); + path = Loki; + sourceTree = ""; + }; D221A07E169C9E5E00537ABF = { isa = PBXGroup; children = ( @@ -3576,6 +3587,7 @@ 454A84042059C787008B8C75 /* MediaTileViewController.swift in Sources */, 340FC8B4204DAC8D007AEB0F /* OWSBackupSettingsViewController.m in Sources */, 34D1F0871F8678AA0066283D /* ConversationViewItem.m in Sources */, + B843951A228510FB000563FE /* Poller.swift in Sources */, 451A13B11E13DED2000A50FD /* AppNotifications.swift in Sources */, 34D99CE4217509C2000AFB39 /* AppEnvironment.swift in Sources */, 348570A820F67575004FF32B /* OWSMessageHeaderView.m in Sources */, diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 35f3641e4..a734e9df9 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -329,7 +329,7 @@ static NSTimeInterval launchStartedAt; [UIApplication.sharedApplication cancelAllLocalNotifications]; [UIApplication.sharedApplication setApplicationIconBadgeNumber:0]; - [[UIApplication sharedApplication] scheduleLocalNotification:notification]; + [UIApplication.sharedApplication scheduleLocalNotification:notification]; [UIApplication.sharedApplication setApplicationIconBadgeNumber:1]; [DDLog flushLog]; @@ -717,6 +717,9 @@ static NSTimeInterval launchStartedAt; [self.socketManager requestSocketOpen]; [Environment.shared.contactsManager fetchSystemContactsOnceIfAlreadyAuthorized]; [[AppEnvironment.shared.messageFetcherJob run] retainUntilComplete]; + + // Loki: Start poller + [Poller.shared startIfNeeded]; // TODO: Ping friends to let them know we're online @@ -1159,13 +1162,6 @@ static NSTimeInterval launchStartedAt; // But once our REST endpoint is fixed to properly de-enqueue fallback notifications, we can easily // use the rest endpoint here rather than the websocket and circumvent making changes to critical code. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - // Show notification (TODO: use actual data for this) - UNMutableNotificationContent *notificationContent = [UNMutableNotificationContent new]; - notificationContent.title = @"Spiderman"; - notificationContent.body = @"Oh hello, can you help me fight crime for a bit?"; - UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:[NSUUID UUID].UUIDString content:notificationContent trigger:nil]; - [UNUserNotificationCenter.currentNotificationCenter addNotificationRequest:notificationRequest withCompletionHandler:nil]; - // Invoke completion handler completionHandler(UIBackgroundFetchResultNewData); job = nil; }); diff --git a/Signal/src/Loki/Poller.swift b/Signal/src/Loki/Poller.swift new file mode 100644 index 000000000..30ca43b2e --- /dev/null +++ b/Signal/src/Loki/Poller.swift @@ -0,0 +1,26 @@ +import PromiseKit + +@objc public final class Poller : NSObject { + private var isStarted = false + private var currentJob: Promise? + + // MARK: Configuration + private static let interval: TimeInterval = 60 + + // MARK: Initialization + @objc public static let shared = Poller() + + private override init() { } + + // MARK: General + @objc public func startIfNeeded() { + guard !isStarted else { return } + Timer.scheduledTimer(timeInterval: Poller.interval, target: self, selector: #selector(poll), userInfo: nil, repeats: true) + isStarted = true + } + + @objc private func poll() { + guard currentJob == nil else { return } + currentJob = AppEnvironment.shared.messageFetcherJob.run().ensure { [weak self] in self?.currentJob = nil } + } +}