move LKPushNotificationManager to SignalServiceKit & add lash hash sync with PN server

pull/149/head
ryanzhao 5 years ago
parent 471870d8a5
commit ec1a468aab

@ -583,7 +583,6 @@
B83F2B88240CB75A000A54AB /* UIImage+Scaling.swift in Sources */ = {isa = PBXBuildFile; fileRef = B83F2B87240CB75A000A54AB /* UIImage+Scaling.swift */; };
B846365B22B7418B00AF1514 /* Identicon+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */; };
B84664F5235022F30083A1CD /* MentionUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B84664F4235022F30083A1CD /* MentionUtilities.swift */; };
B847570323D5698100759540 /* LokiPushNotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = B847570223D5698100759540 /* LokiPushNotificationManager.swift */; };
B85357BF23A1AE0800AAF6CD /* SeedReminderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357BE23A1AE0800AAF6CD /* SeedReminderView.swift */; };
B85357C123A1B81900AAF6CD /* SeedReminderViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357C023A1B81900AAF6CD /* SeedReminderViewDelegate.swift */; };
B85357C323A1BD1200AAF6CD /* SeedVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357C223A1BD1200AAF6CD /* SeedVC.swift */; };
@ -1448,7 +1447,6 @@
B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Identicon+ObjC.swift"; sourceTree = "<group>"; };
B84664F4235022F30083A1CD /* MentionUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MentionUtilities.swift; sourceTree = "<group>"; };
B847570023D568EB00759540 /* SignalServiceKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SignalServiceKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B847570223D5698100759540 /* LokiPushNotificationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LokiPushNotificationManager.swift; sourceTree = "<group>"; };
B85357BE23A1AE0800AAF6CD /* SeedReminderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedReminderView.swift; sourceTree = "<group>"; };
B85357C023A1B81900AAF6CD /* SeedReminderViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedReminderViewDelegate.swift; sourceTree = "<group>"; };
B85357C223A1BD1200AAF6CD /* SeedVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedVC.swift; sourceTree = "<group>"; };
@ -2881,7 +2879,6 @@
isa = PBXGroup;
children = (
B8544E3223D50E4900299F14 /* AppearanceUtilities.swift */,
B847570223D5698100759540 /* LokiPushNotificationManager.swift */,
B84664F4235022F30083A1CD /* MentionUtilities.swift */,
B886B4A82398BA1500211ABE /* QRCode.swift */,
B8783E9D23EB948D00404FB8 /* UILabel+Interaction.swift */,
@ -4033,7 +4030,6 @@
34D99CE4217509C2000AFB39 /* AppEnvironment.swift in Sources */,
348570A820F67575004FF32B /* OWSMessageHeaderView.m in Sources */,
450DF2091E0DD2C6003D14BE /* UserNotificationsAdaptee.swift in Sources */,
B847570323D5698100759540 /* LokiPushNotificationManager.swift in Sources */,
34B6A907218B5241007C4606 /* TypingIndicatorCell.swift in Sources */,
4CFD151D22415AA400F2450F /* CallVideoHintView.swift in Sources */,
34D1F0AB1F867BFC0066283D /* OWSContactOffersCell.m in Sources */,

@ -270,6 +270,9 @@ public final class LokiAPI : NSObject {
private static func updateLastMessageHashValueIfPossible(for target: LokiAPITarget, from rawMessages: [JSON]) {
if let lastMessage = rawMessages.last, let hashValue = lastMessage["hash"] as? String, let expirationDate = lastMessage["expiration"] as? Int {
setLastMessageHashValue(for: target, hashValue: hashValue, expirationDate: UInt64(expirationDate))
if UserDefaults.standard[.isUsingFullAPNs] {
LokiPushNotificationManager.acknowledgeDeliveryForMessage(with: hashValue, hexEncodedPublicKey: userHexEncodedPublicKey)
}
} else if (!rawMessages.isEmpty) {
print("[Loki] Failed to update last message hash value from: \(rawMessages).")
}

@ -1,14 +1,11 @@
// Ideally this should be in SignalServiceKit, but somehow linking fails when it is.
@objc(LKPushNotificationManager)
final class LokiPushNotificationManager : NSObject {
// MARK: Settings
#if DEBUG
private static let url = URL(string: "https://dev.apns.getsession.org/register")!
private static let server = "https://dev.apns.getsession.org/"
#else
private static let url = URL(string: "https://live.apns.getsession.org/register")!
private static let server = "https://live.apns.getsession.org/"
#endif
private static let tokenExpirationInterval: TimeInterval = 2 * 24 * 60 * 60
@ -16,6 +13,8 @@ final class LokiPushNotificationManager : NSObject {
private override init() { }
// MARK: Registration
/** This method is for users to register for Silent Push Notification.
We only need the device token to make the SPN work.*/
@objc(registerWithToken:)
static func register(with token: Data) {
let hexEncodedToken = token.toHexString()
@ -31,6 +30,7 @@ final class LokiPushNotificationManager : NSObject {
return print("[Loki] Using full APNs; no need to upload device token.")
}
let parameters = [ "token" : hexEncodedToken ]
let url = URL(string: server + "register")!
let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
TSNetworkManager.shared().makeRequest(request, success: { _, response in
@ -48,12 +48,15 @@ final class LokiPushNotificationManager : NSObject {
})
}
/** This method is for users to register for Normal Push Notification.
We need the device token and user's public key (session id) to make the NPN work.*/
@objc(registerWithToken:hexEncodedPublicKey:)
static func register(with token: Data, hexEncodedPublicKey: String) {
let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard
let now = Date().timeIntervalSince1970
let parameters = [ "token" : hexEncodedToken, "pubKey" : hexEncodedPublicKey]
let url = URL(string: server + "register")!
let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
TSNetworkManager.shared().makeRequest(request, success: { _, response in
@ -70,4 +73,22 @@ final class LokiPushNotificationManager : NSObject {
print("[Loki] Couldn't register device token.")
})
}
@objc(acknowledgeDeliveryForMessageWithHash:hexEncodedPublicKey:)
static func acknowledgeDeliveryForMessage(with hash: String, hexEncodedPublicKey: String) {
let parameters: [String : Any] = [ "lastHash" : hash, "pubKey" : hexEncodedPublicKey]
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 {
return print("[Loki] Couldn't acknowledge the delivery for message with last hash: " + hash)
}
guard json["code"] as? Int != 0 else {
return print("[Loki] Couldn't acknowledge the delivery for message due to error: \(json["message"] as? String ?? "nil").")
}
}, failure: { _, error in
print("[Loki] Couldn't acknowledge the delivery for message with last hash: " + hash)
})
}
}
Loading…
Cancel
Save