session-ios/Signal/src/Loki/Utilities/LokiPushNotificationManager...

31 lines
1.3 KiB
Swift
Raw Normal View History

2020-01-06 03:16:43 +01:00
import UIKit
2020-01-20 06:00:01 +01:00
// Ideally this should be in SignalServiceKit, but somehow linking fails when it is.
2020-01-06 03:16:43 +01:00
@objc(LKPushNotificationManager)
final class LokiPushNotificationManager : NSObject {
@objc static let shared = LokiPushNotificationManager()
private override init() { super.init() }
@objc(registerWithToken:)
func register(with token: Data) {
let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined()
print("Registering device token: (\(hexEncodedToken))")
// Send token to Loki server
let parameters = [ "token" : hexEncodedToken ]
let url = URL(string: "https://live.apns.getsession.org/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
guard let json = response as? JSON else { return }
guard json["code"] as? Int != 0 else {
return print("[Loki] An error occured during device token registration: \(json["message"] as? String ?? "nil").")
}
}, failure: { _, error in
print("[Loki] Couldn't register device token.")
})
}
}