|
|
|
@ -24,33 +24,21 @@ public final class LokiPushNotificationManager : NSObject {
|
|
|
|
|
/// Unregisters the user from push notifications. Only the user's device token is needed for this.
|
|
|
|
|
static func unregister(with token: Data, isForcedUpdate: Bool) -> Promise<Void> {
|
|
|
|
|
let hexEncodedToken = token.toHexString()
|
|
|
|
|
let userDefaults = UserDefaults.standard
|
|
|
|
|
let oldToken = userDefaults[.deviceToken]
|
|
|
|
|
let lastUploadTime = userDefaults[.lastDeviceTokenUpload2]
|
|
|
|
|
let isUsingFullAPNs = userDefaults[.isUsingFullAPNs]
|
|
|
|
|
let now = Date().timeIntervalSince1970
|
|
|
|
|
guard isForcedUpdate || hexEncodedToken != oldToken || now - lastUploadTime > tokenExpirationInterval else {
|
|
|
|
|
print("[Loki] Device token hasn't changed or expired; no need to re-upload.")
|
|
|
|
|
return Promise<Void> { $0.fulfill(()) }
|
|
|
|
|
}
|
|
|
|
|
let parameters = [ "token" : hexEncodedToken ]
|
|
|
|
|
let url = URL(string: "\(server)/unregister")!
|
|
|
|
|
let request = TSRequest(url: url, method: "POST", parameters: parameters)
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
|
|
|
|
|
let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: pnServerPublicKey).map2 { response in
|
|
|
|
|
guard let json = response as? JSON else {
|
|
|
|
|
return print("[Loki] Couldn't register device token.")
|
|
|
|
|
return print("[Loki] Couldn't unregister from push notifications.")
|
|
|
|
|
}
|
|
|
|
|
guard json["code"] as? Int != 0 else {
|
|
|
|
|
return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").")
|
|
|
|
|
return print("[Loki] Couldn't unregister from push notifications due to error: \(json["message"] as? String ?? "nil").")
|
|
|
|
|
}
|
|
|
|
|
userDefaults[.deviceToken] = hexEncodedToken
|
|
|
|
|
userDefaults[.lastDeviceTokenUpload2] = now
|
|
|
|
|
userDefaults[.isUsingFullAPNs] = false
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
promise.catch2 { error in
|
|
|
|
|
print("[Loki] Couldn't register device token.")
|
|
|
|
|
print("[Loki] Couldn't unregister from push notifications.")
|
|
|
|
|
}
|
|
|
|
|
// Unsubscribe from all closed groups
|
|
|
|
|
Storage.getUserClosedGroupPublicKeys().forEach { closedGroup in
|
|
|
|
|