2020-04-07 01:33:29 +02:00
|
|
|
|
2020-01-06 03:16:43 +01:00
|
|
|
@objc(LKPushNotificationManager)
|
|
|
|
final class LokiPushNotificationManager : NSObject {
|
2020-03-27 05:08:26 +01:00
|
|
|
|
|
|
|
// MARK: Settings
|
|
|
|
#if DEBUG
|
2020-03-30 08:07:25 +02:00
|
|
|
private static let server = "https://dev.apns.getsession.org/"
|
2020-03-27 05:08:26 +01:00
|
|
|
#else
|
2020-03-30 08:07:25 +02:00
|
|
|
private static let server = "https://live.apns.getsession.org/"
|
2020-03-27 05:08:26 +01:00
|
|
|
#endif
|
|
|
|
private static let tokenExpirationInterval: TimeInterval = 2 * 24 * 60 * 60
|
|
|
|
|
|
|
|
// MARK: Initialization
|
|
|
|
private override init() { }
|
|
|
|
|
|
|
|
// MARK: Registration
|
2020-04-07 01:33:29 +02:00
|
|
|
/// Registers the user for silent push notifications (that then trigger the app
|
|
|
|
/// into fetching messages). Only the user's device token is needed for this.
|
2020-01-06 03:16:43 +01:00
|
|
|
@objc(registerWithToken:)
|
2020-03-27 05:08:26 +01:00
|
|
|
static func register(with token: Data) {
|
|
|
|
let hexEncodedToken = token.toHexString()
|
2020-02-19 06:45:38 +01:00
|
|
|
let userDefaults = UserDefaults.standard
|
|
|
|
let oldToken = userDefaults[.deviceToken]
|
|
|
|
let lastUploadTime = userDefaults[.lastDeviceTokenUpload]
|
2020-03-27 05:08:26 +01:00
|
|
|
let isUsingFullAPNs = userDefaults[.isUsingFullAPNs]
|
2020-02-11 03:37:03 +01:00
|
|
|
let now = Date().timeIntervalSince1970
|
2020-03-27 05:08:26 +01:00
|
|
|
guard hexEncodedToken != oldToken || now - lastUploadTime < tokenExpirationInterval else {
|
|
|
|
return print("[Loki] Device token hasn't changed; no need to re-upload.")
|
2020-02-06 23:35:40 +01:00
|
|
|
}
|
2020-03-27 05:08:26 +01:00
|
|
|
guard !isUsingFullAPNs else {
|
2020-03-31 07:34:31 +02:00
|
|
|
return print("[Loki] Using full APNs; ignoring call to register(with:).")
|
2020-03-25 01:50:46 +01:00
|
|
|
}
|
2020-01-06 03:16:43 +01:00
|
|
|
let parameters = [ "token" : hexEncodedToken ]
|
2020-03-30 08:07:25 +02:00
|
|
|
let url = URL(string: server + "register")!
|
2020-01-06 03:16:43 +01:00
|
|
|
let request = TSRequest(url: url, method: "POST", parameters: parameters)
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
|
|
|
|
TSNetworkManager.shared().makeRequest(request, success: { _, response in
|
2020-03-27 05:08:26 +01:00
|
|
|
guard let json = response as? JSON else {
|
|
|
|
return print("[Loki] Couldn't register device token.")
|
|
|
|
}
|
2020-01-06 03:16:43 +01:00
|
|
|
guard json["code"] as? Int != 0 else {
|
2020-03-27 05:08:26 +01:00
|
|
|
return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").")
|
2020-01-06 03:16:43 +01:00
|
|
|
}
|
2020-02-19 06:45:38 +01:00
|
|
|
userDefaults[.deviceToken] = hexEncodedToken
|
|
|
|
userDefaults[.lastDeviceTokenUpload] = now
|
2020-03-27 05:08:26 +01:00
|
|
|
userDefaults[.isUsingFullAPNs] = false
|
2020-03-25 01:50:46 +01:00
|
|
|
}, failure: { _, error in
|
|
|
|
print("[Loki] Couldn't register device token.")
|
|
|
|
})
|
|
|
|
}
|
2020-04-07 01:33:29 +02:00
|
|
|
|
|
|
|
/// Registers the user for normal push notifications. Requires the user's device
|
|
|
|
/// token and their Session ID.
|
2020-03-27 05:08:26 +01:00
|
|
|
@objc(registerWithToken:hexEncodedPublicKey:)
|
|
|
|
static func register(with token: Data, hexEncodedPublicKey: String) {
|
|
|
|
let hexEncodedToken = token.toHexString()
|
2020-03-25 01:50:46 +01:00
|
|
|
let userDefaults = UserDefaults.standard
|
|
|
|
let now = Date().timeIntervalSince1970
|
2020-03-27 05:08:26 +01:00
|
|
|
let parameters = [ "token" : hexEncodedToken, "pubKey" : hexEncodedPublicKey]
|
2020-03-30 08:07:25 +02:00
|
|
|
let url = URL(string: server + "register")!
|
2020-03-25 01:50:46 +01:00
|
|
|
let request = TSRequest(url: url, method: "POST", parameters: parameters)
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
|
|
|
|
TSNetworkManager.shared().makeRequest(request, success: { _, response in
|
2020-03-27 05:08:26 +01:00
|
|
|
guard let json = response as? JSON else {
|
|
|
|
return print("[Loki] Couldn't register device token.")
|
|
|
|
}
|
2020-03-25 01:50:46 +01:00
|
|
|
guard json["code"] as? Int != 0 else {
|
2020-03-27 05:08:26 +01:00
|
|
|
return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").")
|
2020-03-25 01:50:46 +01:00
|
|
|
}
|
|
|
|
userDefaults[.deviceToken] = hexEncodedToken
|
|
|
|
userDefaults[.lastDeviceTokenUpload] = now
|
2020-03-27 05:08:26 +01:00
|
|
|
userDefaults[.isUsingFullAPNs] = true
|
2020-01-06 03:16:43 +01:00
|
|
|
}, failure: { _, error in
|
|
|
|
print("[Loki] Couldn't register device token.")
|
|
|
|
})
|
|
|
|
}
|
2020-03-30 08:07:25 +02:00
|
|
|
|
2020-04-02 02:57:25 +02:00
|
|
|
@objc(acknowledgeDeliveryForMessageWithHash:expiration:hexEncodedPublicKey:)
|
2020-04-07 01:33:29 +02:00
|
|
|
static func acknowledgeDelivery(forMessageWithHash hash: String, expiration: Int, hexEncodedPublicKey: String) {
|
|
|
|
let parameters: JSON = [ "lastHash" : hash, "pubKey" : hexEncodedPublicKey, "expiration" : expiration]
|
2020-03-30 08:07:25 +02:00
|
|
|
let url = URL(string: server + "acknowledge_message_delivery")!
|
|
|
|
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 {
|
2020-04-07 01:33:29 +02:00
|
|
|
return print("[Loki] Couldn't acknowledge delivery for message with hash: \(hash).")
|
2020-03-30 08:07:25 +02:00
|
|
|
}
|
|
|
|
guard json["code"] as? Int != 0 else {
|
2020-04-07 01:33:29 +02:00
|
|
|
return print("[Loki] Couldn't acknowledge delivery for message with hash: \(hash) due to error: \(json["message"] as? String ?? "nil").")
|
2020-03-30 08:07:25 +02:00
|
|
|
}
|
|
|
|
}, failure: { _, error in
|
2020-04-07 01:33:29 +02:00
|
|
|
print("[Loki] Couldn't acknowledge delivery for message with hash: \(hash).")
|
2020-03-30 08:07:25 +02:00
|
|
|
})
|
|
|
|
}
|
2020-01-06 03:16:43 +01:00
|
|
|
}
|