mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
6.1 KiB
Swift
123 lines
6.1 KiB
Swift
6 years ago
|
import PromiseKit
|
||
6 years ago
|
|
||
6 years ago
|
@objc(LKPushNotificationManager)
|
||
6 years ago
|
public final class LokiPushNotificationManager : NSObject {
|
||
6 years ago
|
|
||
|
// MARK: Settings
|
||
|
#if DEBUG
|
||
6 years ago
|
private static let server = "https://dev.apns.getsession.org/"
|
||
6 years ago
|
#else
|
||
6 years ago
|
private static let server = "https://live.apns.getsession.org/"
|
||
6 years ago
|
#endif
|
||
6 years ago
|
private static let tokenExpirationInterval: TimeInterval = 12 * 60 * 60
|
||
6 years ago
|
|
||
|
// MARK: Initialization
|
||
|
private override init() { }
|
||
|
|
||
|
// MARK: Registration
|
||
6 years ago
|
/// 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.
|
||
6 years ago
|
static func register(with token: Data, isForcedUpdate: Bool) -> Promise<Void> {
|
||
6 years ago
|
let hexEncodedToken = token.toHexString()
|
||
6 years ago
|
let userDefaults = UserDefaults.standard
|
||
|
let oldToken = userDefaults[.deviceToken]
|
||
|
let lastUploadTime = userDefaults[.lastDeviceTokenUpload]
|
||
6 years ago
|
let isUsingFullAPNs = userDefaults[.isUsingFullAPNs]
|
||
6 years ago
|
let now = Date().timeIntervalSince1970
|
||
6 years ago
|
guard isForcedUpdate || hexEncodedToken != oldToken || now - lastUploadTime > tokenExpirationInterval else {
|
||
6 years ago
|
print("[Loki] Device token hasn't changed; no need to re-upload.")
|
||
|
return Promise<Void> { $0.fulfill(()) }
|
||
6 years ago
|
}
|
||
6 years ago
|
guard !isUsingFullAPNs else {
|
||
6 years ago
|
print("[Loki] Using full APNs; ignoring call to register(with:).")
|
||
|
return Promise<Void> { $0.fulfill(()) }
|
||
6 years ago
|
}
|
||
6 years ago
|
let parameters = [ "token" : hexEncodedToken ]
|
||
6 years ago
|
let url = URL(string: server + "register")!
|
||
6 years ago
|
let request = TSRequest(url: url, method: "POST", parameters: parameters)
|
||
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
|
||
6 years ago
|
let promise = TSNetworkManager.shared().makePromise(request: request).map { _, response in
|
||
6 years ago
|
guard let json = response as? JSON else {
|
||
|
return print("[Loki] Couldn't register device token.")
|
||
|
}
|
||
6 years ago
|
guard json["code"] as? Int != 0 else {
|
||
6 years ago
|
return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").")
|
||
6 years ago
|
}
|
||
6 years ago
|
userDefaults[.deviceToken] = hexEncodedToken
|
||
|
userDefaults[.lastDeviceTokenUpload] = now
|
||
6 years ago
|
userDefaults[.isUsingFullAPNs] = false
|
||
6 years ago
|
return
|
||
|
}
|
||
|
promise.catch { error in
|
||
6 years ago
|
print("[Loki] Couldn't register device token.")
|
||
6 years ago
|
}
|
||
|
return promise
|
||
|
}
|
||
|
|
||
|
/// 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.
|
||
6 years ago
|
@objc(registerWithToken:isForcedUpdate:)
|
||
|
static func objc_register(with token: Data, isForcedUpdate: Bool) -> AnyPromise {
|
||
|
return AnyPromise.from(register(with: token, isForcedUpdate: isForcedUpdate))
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
/// Registers the user for normal push notifications. Requires the user's device
|
||
|
/// token and their Session ID.
|
||
6 years ago
|
static func register(with token: Data, hexEncodedPublicKey: String, isForcedUpdate: Bool) -> Promise<Void> {
|
||
6 years ago
|
let hexEncodedToken = token.toHexString()
|
||
6 years ago
|
let userDefaults = UserDefaults.standard
|
||
6 years ago
|
let oldToken = userDefaults[.deviceToken]
|
||
|
let lastUploadTime = userDefaults[.lastDeviceTokenUpload]
|
||
6 years ago
|
let now = Date().timeIntervalSince1970
|
||
6 years ago
|
guard isForcedUpdate || hexEncodedToken != oldToken || now - lastUploadTime > tokenExpirationInterval else {
|
||
|
print("[Loki] Device token hasn't changed; no need to re-upload.")
|
||
|
return Promise<Void> { $0.fulfill(()) }
|
||
|
}
|
||
6 years ago
|
let parameters = [ "token" : hexEncodedToken, "pubKey" : hexEncodedPublicKey]
|
||
6 years ago
|
let url = URL(string: server + "register")!
|
||
6 years ago
|
let request = TSRequest(url: url, method: "POST", parameters: parameters)
|
||
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
|
||
6 years ago
|
let promise = TSNetworkManager.shared().makePromise(request: request).map { _, response in
|
||
6 years ago
|
guard let json = response as? JSON else {
|
||
|
return print("[Loki] Couldn't register device token.")
|
||
|
}
|
||
6 years ago
|
guard json["code"] as? Int != 0 else {
|
||
6 years ago
|
return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").")
|
||
6 years ago
|
}
|
||
|
userDefaults[.deviceToken] = hexEncodedToken
|
||
|
userDefaults[.lastDeviceTokenUpload] = now
|
||
6 years ago
|
userDefaults[.isUsingFullAPNs] = true
|
||
6 years ago
|
return
|
||
|
}
|
||
|
promise.catch { error in
|
||
6 years ago
|
print("[Loki] Couldn't register device token.")
|
||
6 years ago
|
}
|
||
|
return promise
|
||
|
}
|
||
|
|
||
6 years ago
|
/// Registers the user for normal push notifications. Requires the user's device
|
||
|
/// token and their Session ID.
|
||
6 years ago
|
@objc(registerWithToken:hexEncodedPublicKey:isForcedUpdate:)
|
||
|
static func objc_register(with token: Data, hexEncodedPublicKey: String, isForcedUpdate: Bool) -> AnyPromise {
|
||
|
return AnyPromise.from(register(with: token, hexEncodedPublicKey: hexEncodedPublicKey, isForcedUpdate: isForcedUpdate))
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
@objc(acknowledgeDeliveryForMessageWithHash:expiration:hexEncodedPublicKey:)
|
||
6 years ago
|
static func acknowledgeDelivery(forMessageWithHash hash: String, expiration: Int, hexEncodedPublicKey: String) {
|
||
|
let parameters: JSON = [ "lastHash" : hash, "pubKey" : hexEncodedPublicKey, "expiration" : expiration]
|
||
6 years ago
|
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 {
|
||
6 years ago
|
return print("[Loki] Couldn't acknowledge delivery for message with hash: \(hash).")
|
||
6 years ago
|
}
|
||
|
guard json["code"] as? Int != 0 else {
|
||
6 years ago
|
return print("[Loki] Couldn't acknowledge delivery for message with hash: \(hash) due to error: \(json["message"] as? String ?? "nil").")
|
||
6 years ago
|
}
|
||
|
}, failure: { _, error in
|
||
6 years ago
|
print("[Loki] Couldn't acknowledge delivery for message with hash: \(hash).")
|
||
6 years ago
|
})
|
||
|
}
|
||
6 years ago
|
}
|