From c7f55ad0f7d884c7e6784cca7a819da7935676c9 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Fri, 7 Feb 2020 09:35:40 +1100 Subject: [PATCH] Do not upload duplicated device token --- Signal/src/AppDelegate.m | 2 +- .../src/Loki/Utilities/LokiPushNotificationManager.swift | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 68674e373..42944860a 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -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 diff --git a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift index 586339ce3..8e8097f6e 100644 --- a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift +++ b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift @@ -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.") })