Refactor LokiPushNotificationManager

pull/147/head
gmbnt 5 years ago
parent 552c49dd5a
commit 25903554e9

@ -588,7 +588,7 @@ static BOOL isInternalTestVersion = NO;
OWSLogInfo(@"Registered for push notifications with token: %@.", deviceToken); OWSLogInfo(@"Registered for push notifications with token: %@.", deviceToken);
//TODO: For normal push notification test //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]; // [LKPushNotificationManager.shared registerWithToken:deviceToken];
// [self.pushRegistrationManager didReceiveVanillaPushToken:deviceToken]; // [self.pushRegistrationManager didReceiveVanillaPushToken:deviceToken];
} }

@ -1,75 +1,71 @@
import UIKit
// Ideally this should be in SignalServiceKit, but somehow linking fails when it is. // Ideally this should be in SignalServiceKit, but somehow linking fails when it is.
@objc(LKPushNotificationManager) @objc(LKPushNotificationManager)
final class LokiPushNotificationManager : NSObject { final class LokiPushNotificationManager : NSObject {
@objc static let shared = LokiPushNotificationManager() // 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
private override init() { super.init() } // MARK: Initialization
private override init() { }
// MARK: Registration
@objc(registerWithToken:) @objc(registerWithToken:)
func register(with token: Data) { static func register(with token: Data) {
let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined() let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard let userDefaults = UserDefaults.standard
let oldToken = userDefaults[.deviceToken] let oldToken = userDefaults[.deviceToken]
let lastUploadTime = userDefaults[.lastDeviceTokenUpload] let lastUploadTime = userDefaults[.lastDeviceTokenUpload]
let applyNormalNotification = userDefaults[.applyNormalNotification] let isUsingFullAPNs = userDefaults[.isUsingFullAPNs]
let now = Date().timeIntervalSince1970 let now = Date().timeIntervalSince1970
if hexEncodedToken == oldToken && now - lastUploadTime < 2 * 24 * 60 * 60 { guard hexEncodedToken != oldToken || now - lastUploadTime < tokenExpirationInterval else {
print("[Loki] Device token hasn't changed; no need to upload.") return print("[Loki] Device token hasn't changed; no need to re-upload.")
return
} }
if applyNormalNotification { guard !isUsingFullAPNs else {
print("[Loki] Using normal notification; no need to upload.") return print("[Loki] Using full APNs; no need to upload device token.")
return
} }
// Send token to Loki server
let parameters = [ "token" : hexEncodedToken ] 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) let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
TSNetworkManager.shared().makeRequest(request, success: { _, response in 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 { 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[.deviceToken] = hexEncodedToken
userDefaults[.lastDeviceTokenUpload] = now userDefaults[.lastDeviceTokenUpload] = now
userDefaults[.applyNormalNotification] = false userDefaults[.isUsingFullAPNs] = false
}, failure: { _, error in }, failure: { _, error in
print("[Loki] Couldn't register device token.") print("[Loki] Couldn't register device token.")
}) })
} }
@objc(registerWithToken: pubkey:) @objc(registerWithToken:hexEncodedPublicKey:)
func register(with token: Data, pubkey: String) { static func register(with token: Data, hexEncodedPublicKey: String) {
let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined() let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard let userDefaults = UserDefaults.standard
let now = Date().timeIntervalSince1970 let now = Date().timeIntervalSince1970
// Send token to Loki server let parameters = [ "token" : hexEncodedToken, "pubKey" : hexEncodedPublicKey]
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 request = TSRequest(url: url, method: "POST", parameters: parameters) let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
TSNetworkManager.shared().makeRequest(request, success: { _, response in 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 { 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[.deviceToken] = hexEncodedToken
userDefaults[.lastDeviceTokenUpload] = now userDefaults[.lastDeviceTokenUpload] = now
userDefaults[.applyNormalNotification] = true userDefaults[.isUsingFullAPNs] = true
}, failure: { _, error in }, failure: { _, error in
print("[Loki] Couldn't register device token.") print("[Loki] Couldn't register device token.")
}) })

@ -8,7 +8,7 @@ public enum LKUserDefaults {
case hasViewedSeed case hasViewedSeed
/// Whether the device was unlinked as a slave device (used to notify the user on the landing screen). /// Whether the device was unlinked as a slave device (used to notify the user on the landing screen).
case wasUnlinked case wasUnlinked
case applyNormalNotification case isUsingFullAPNs
} }
public enum Date : Swift.String { public enum Date : Swift.String {

Loading…
Cancel
Save