Do not upload duplicated device token

This commit is contained in:
Ryan ZHAO 2020-02-07 09:35:40 +11:00
parent 24f31081d1
commit c7f55ad0f7
2 changed files with 7 additions and 2 deletions

View File

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

View File

@ -12,7 +12,11 @@ final class LokiPushNotificationManager : NSObject {
@objc(registerWithToken:)
func register(with token: Data) {
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
let parameters = [ "token" : hexEncodedToken ]
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 {
return print("[Loki] An error occured during device token registration: \(json["message"] as? String ?? "nil").")
}
UserDefaults.standard.set(hexEncodedToken, forKey: "deviceToken")
}, failure: { _, error in
print("[Loki] Couldn't register device token.")
})