From 25903554e93914d615cb6c38bfe6bfb4d22c4d84 Mon Sep 17 00:00:00 2001 From: gmbnt Date: Fri, 27 Mar 2020 15:08:26 +1100 Subject: [PATCH] Refactor LokiPushNotificationManager --- Signal/src/AppDelegate.m | 2 +- .../LokiPushNotificationManager.swift | 72 +++++++++---------- .../src/Loki/Utilities/LKUserDefaults.swift | 2 +- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 0c52c7248..ed046fc18 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -588,7 +588,7 @@ static BOOL isInternalTestVersion = NO; OWSLogInfo(@"Registered for push notifications with token: %@.", deviceToken); //TODO: For normal push notification test - [LKPushNotificationManager.shared registerWithToken:deviceToken pubkey:self.tsAccountManager.localNumber]; + [LKPushNotificationManager registerWithToken:deviceToken hexEncodedPublicKey:self.tsAccountManager.localNumber]; // [LKPushNotificationManager.shared registerWithToken:deviceToken]; // [self.pushRegistrationManager didReceiveVanillaPushToken:deviceToken]; } diff --git a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift index c5b65f8df..d37c71e7d 100644 --- a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift +++ b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift @@ -1,75 +1,71 @@ -import UIKit // Ideally this should be in SignalServiceKit, but somehow linking fails when it is. @objc(LKPushNotificationManager) final class LokiPushNotificationManager : NSObject { - - @objc static let shared = LokiPushNotificationManager() - - private override init() { super.init() } - + + // MARK: Settings + #if DEBUG + private static let url = URL(string: "https://dev.apns.getsession.org/register")! + #else + private static let url = URL(string: "https://live.apns.getsession.org/register")! + #endif + private static let tokenExpirationInterval: TimeInterval = 2 * 24 * 60 * 60 + + // MARK: Initialization + private override init() { } + + // MARK: Registration @objc(registerWithToken:) - func register(with token: Data) { - let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined() + static func register(with token: Data) { + let hexEncodedToken = token.toHexString() let userDefaults = UserDefaults.standard let oldToken = userDefaults[.deviceToken] let lastUploadTime = userDefaults[.lastDeviceTokenUpload] - let applyNormalNotification = userDefaults[.applyNormalNotification] + let isUsingFullAPNs = userDefaults[.isUsingFullAPNs] let now = Date().timeIntervalSince1970 - if hexEncodedToken == oldToken && now - lastUploadTime < 2 * 24 * 60 * 60 { - print("[Loki] Device token hasn't changed; no need to upload.") - return + guard hexEncodedToken != oldToken || now - lastUploadTime < tokenExpirationInterval else { + return print("[Loki] Device token hasn't changed; no need to re-upload.") } - if applyNormalNotification { - print("[Loki] Using normal notification; no need to upload.") - return + guard !isUsingFullAPNs else { + return print("[Loki] Using full APNs; no need to upload device token.") } - // Send token to Loki server let parameters = [ "token" : hexEncodedToken ] - #if DEBUG - let url = URL(string: "https://dev.apns.getsession.org/register")! - #else - let url = URL(string: "https://live.apns.getsession.org/register")! - #endif let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] TSNetworkManager.shared().makeRequest(request, success: { _, response in - guard let json = response as? JSON else { return } + guard let json = response as? JSON else { + return print("[Loki] Couldn't register device token.") + } guard json["code"] as? Int != 0 else { - return print("[Loki] An error occured during device token registration: \(json["message"] as? String ?? "nil").") + return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").") } userDefaults[.deviceToken] = hexEncodedToken userDefaults[.lastDeviceTokenUpload] = now - userDefaults[.applyNormalNotification] = false + userDefaults[.isUsingFullAPNs] = false }, failure: { _, error in print("[Loki] Couldn't register device token.") }) } - @objc(registerWithToken: pubkey:) - func register(with token: Data, pubkey: String) { - let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined() + @objc(registerWithToken:hexEncodedPublicKey:) + static func register(with token: Data, hexEncodedPublicKey: String) { + let hexEncodedToken = token.toHexString() let userDefaults = UserDefaults.standard let now = Date().timeIntervalSince1970 - // Send token to Loki server - let parameters = [ "token" : hexEncodedToken, - "pubKey": pubkey] - #if DEBUG - let url = URL(string: "https://dev.apns.getsession.org/register")! - #else - let url = URL(string: "https://live.apns.getsession.org/register")! - #endif + let parameters = [ "token" : hexEncodedToken, "pubKey" : hexEncodedPublicKey] let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] TSNetworkManager.shared().makeRequest(request, success: { _, response in - guard let json = response as? JSON else { return } + guard let json = response as? JSON else { + return print("[Loki] Couldn't register device token.") + } guard json["code"] as? Int != 0 else { - return print("[Loki] An error occured during device token registration: \(json["message"] as? String ?? "nil").") + return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").") } userDefaults[.deviceToken] = hexEncodedToken userDefaults[.lastDeviceTokenUpload] = now - userDefaults[.applyNormalNotification] = true + userDefaults[.isUsingFullAPNs] = true }, failure: { _, error in print("[Loki] Couldn't register device token.") }) diff --git a/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift b/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift index 4a21e975e..bcaefcdfb 100644 --- a/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift +++ b/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift @@ -8,7 +8,7 @@ public enum LKUserDefaults { case hasViewedSeed /// Whether the device was unlinked as a slave device (used to notify the user on the landing screen). case wasUnlinked - case applyNormalNotification + case isUsingFullAPNs } public enum Date : Swift.String {