|
|
|
@ -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()
|
|
|
|
|
// 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:)
|
|
|
|
|
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.")
|
|
|
|
|
})
|
|
|
|
|