Do not upload duplicated device token

pull/87/head
Ryan ZHAO 5 years ago
parent 24f31081d1
commit c7f55ad0f7

@ -580,7 +580,7 @@ static NSTimeInterval launchStartedAt;
OWSLogInfo(@"Registered for push notifications with token: %@.", deviceToken); OWSLogInfo(@"Registered for push notifications with token: %@.", deviceToken);
[LKPushNotificationManager.shared registerWithToken:deviceToken]; [LKPushNotificationManager.shared registerWithToken:deviceToken];
[self.pushRegistrationManager didReceiveVanillaPushToken:deviceToken]; // [self.pushRegistrationManager didReceiveVanillaPushToken:deviceToken];
} }
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

@ -12,7 +12,11 @@ final class LokiPushNotificationManager : NSObject {
@objc(registerWithToken:) @objc(registerWithToken:)
func register(with token: Data) { func register(with token: Data) {
let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined() let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined()
print("Registering device token: (\(hexEncodedToken))") let oldToken = UserDefaults.standard.string(forKey: "deviceToken")
if (hexEncodedToken == oldToken) {
Logger.info("Token is not changed, no need to upload")
return
}
// Send token to Loki server // Send token to Loki server
let parameters = [ "token" : hexEncodedToken ] let parameters = [ "token" : hexEncodedToken ]
let url = URL(string: "https://live.apns.getsession.org/register")! let url = URL(string: "https://live.apns.getsession.org/register")!
@ -23,6 +27,7 @@ final class LokiPushNotificationManager : NSObject {
guard json["code"] as? Int != 0 else { guard json["code"] as? Int != 0 else {
return print("[Loki] An error occured during device token registration: \(json["message"] as? String ?? "nil").") return print("[Loki] An error occured during device token registration: \(json["message"] as? String ?? "nil").")
} }
UserDefaults.standard.set(hexEncodedToken, forKey: "deviceToken")
}, failure: { _, error in }, failure: { _, error in
print("[Loki] Couldn't register device token.") print("[Loki] Couldn't register device token.")
}) })

Loading…
Cancel
Save