Fix APNs token updating bug

pull/160/head
nielsandriesse 5 years ago
parent afd560d906
commit fc9a678181

@ -590,9 +590,9 @@ static BOOL isInternalTestVersion = NO;
OWSLogInfo(@"Registering for push notifications with token: %@.", deviceToken); OWSLogInfo(@"Registering for push notifications with token: %@.", deviceToken);
BOOL isUsingFullAPNs = [NSUserDefaults.standardUserDefaults boolForKey:@"isUsingFullAPNs"]; BOOL isUsingFullAPNs = [NSUserDefaults.standardUserDefaults boolForKey:@"isUsingFullAPNs"];
if (isUsingFullAPNs) { if (isUsingFullAPNs) {
__unused AnyPromise *promise = [LKPushNotificationManager registerWithToken:deviceToken hexEncodedPublicKey:self.tsAccountManager.localNumber]; __unused AnyPromise *promise = [LKPushNotificationManager registerWithToken:deviceToken hexEncodedPublicKey:self.tsAccountManager.localNumber isForcedUpdate:NO];
} else { } else {
__unused AnyPromise *promise = [LKPushNotificationManager registerWithToken:deviceToken]; __unused AnyPromise *promise = [LKPushNotificationManager registerWithToken:deviceToken isForcedUpdate:NO];
} }
} }

@ -59,7 +59,7 @@ class SyncPushTokensJob: NSObject {
Logger.warn("uploading tokens to account servers. pushToken: \(redact(pushToken)), voipToken: \(redact(voipToken))") Logger.warn("uploading tokens to account servers. pushToken: \(redact(pushToken)), voipToken: \(redact(voipToken))")
return firstly { return firstly {
self.accountManager.updatePushTokens(pushToken: pushToken, voipToken: voipToken) self.accountManager.updatePushTokens(pushToken: pushToken, voipToken: voipToken, isForcedUpdate: shouldUploadTokens)
}.done { _ in }.done { _ in
self.recordPushTokensLocally(pushToken: pushToken, voipToken: voipToken) self.recordPushTokensLocally(pushToken: pushToken, voipToken: voipToken)
} }

@ -76,7 +76,9 @@ final class PNModeSheet : Sheet, OptionViewDelegate {
return present(alert, animated: true, completion: nil) return present(alert, animated: true, completion: nil)
} }
UserDefaults.standard[.isUsingFullAPNs] = (selectedOptionView == apnsOptionView) UserDefaults.standard[.isUsingFullAPNs] = (selectedOptionView == apnsOptionView)
let _: Promise<Void> = SyncPushTokensJob.run(accountManager: AppEnvironment.shared.accountManager, preferences: Environment.shared.preferences) let syncTokensJob = SyncPushTokensJob(accountManager: AppEnvironment.shared.accountManager, preferences: Environment.shared.preferences)
syncTokensJob.uploadOnlyIfStale = false
let _: Promise<Void> = syncTokensJob.run()
close() close()
} }

@ -98,6 +98,8 @@ final class PNModeVC : BaseVC, OptionViewDelegate {
TSAccountManager.sharedInstance().didRegister() TSAccountManager.sharedInstance().didRegister()
let homeVC = HomeVC() let homeVC = HomeVC()
navigationController!.setViewControllers([ homeVC ], animated: true) navigationController!.setViewControllers([ homeVC ], animated: true)
let _: Promise<Void> = SyncPushTokensJob.run(accountManager: AppEnvironment.shared.accountManager, preferences: Environment.shared.preferences) let syncTokensJob = SyncPushTokensJob(accountManager: AppEnvironment.shared.accountManager, preferences: Environment.shared.preferences)
syncTokensJob.uploadOnlyIfStale = false
let _: Promise<Void> = syncTokensJob.run()
} }
} }

@ -105,12 +105,13 @@ public class AccountManager: NSObject {
// MARK: Message Delivery // MARK: Message Delivery
func updatePushTokens(pushToken: String, voipToken: String) -> Promise<Void> { func updatePushTokens(pushToken: String, voipToken: String, isForcedUpdate: Bool) -> Promise<Void> {
return Promise { resolver in return Promise { resolver in
tsAccountManager.registerForPushNotifications(pushToken: pushToken, tsAccountManager.registerForPushNotifications(pushToken: pushToken,
voipToken: voipToken, voipToken: voipToken,
success: { resolver.fulfill(()) }, isForcedUpdate: isForcedUpdate,
failure: resolver.reject) success: { resolver.fulfill(()) },
failure: resolver.reject)
} }
} }

@ -137,7 +137,9 @@
- (void)didToggleAPNsSwitch:(UISwitch *)sender - (void)didToggleAPNsSwitch:(UISwitch *)sender
{ {
[NSUserDefaults.standardUserDefaults setBool:sender.on forKey:@"isUsingFullAPNs"]; [NSUserDefaults.standardUserDefaults setBool:sender.on forKey:@"isUsingFullAPNs"];
__unused AnyPromise *promise = [OWSSyncPushTokensJob runWithAccountManager:AppEnvironment.shared.accountManager preferences:Environment.shared.preferences]; OWSSyncPushTokensJob *syncTokensJob = [[OWSSyncPushTokensJob alloc] initWithAccountManager:AppEnvironment.shared.accountManager preferences:Environment.shared.preferences];
syncTokensJob.uploadOnlyIfStale = NO;
__unused AnyPromise *promise = [syncTokensJob run];
} }
@end @end

@ -120,9 +120,10 @@ typedef NS_ENUM(NSUInteger, OWSRegistrationState) {
*/ */
- (void)registerForPushNotificationsWithPushToken:(NSString *)pushToken - (void)registerForPushNotificationsWithPushToken:(NSString *)pushToken
voipToken:(NSString *)voipToken voipToken:(NSString *)voipToken
isForcedUpdate:(BOOL)isForcedUpdate
success:(void (^)(void))successHandler success:(void (^)(void))successHandler
failure:(void (^)(NSError *error))failureHandler failure:(void (^)(NSError *error))failureHandler
NS_SWIFT_NAME(registerForPushNotifications(pushToken:voipToken:success:failure:)); NS_SWIFT_NAME(registerForPushNotifications(pushToken:voipToken:isForcedUpdate:success:failure:));
#endif #endif

@ -280,11 +280,13 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
- (void)registerForPushNotificationsWithPushToken:(NSString *)pushToken - (void)registerForPushNotificationsWithPushToken:(NSString *)pushToken
voipToken:(NSString *)voipToken voipToken:(NSString *)voipToken
isForcedUpdate:(BOOL)isForcedUpdate
success:(void (^)(void))successHandler success:(void (^)(void))successHandler
failure:(void (^)(NSError *))failureHandler failure:(void (^)(NSError *))failureHandler
{ {
[self registerForPushNotificationsWithPushToken:pushToken [self registerForPushNotificationsWithPushToken:pushToken
voipToken:voipToken voipToken:voipToken
isForcedUpdate:isForcedUpdate
success:successHandler success:successHandler
failure:failureHandler failure:failureHandler
remainingRetries:3]; remainingRetries:3];
@ -292,20 +294,21 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
- (void)registerForPushNotificationsWithPushToken:(NSString *)pushToken - (void)registerForPushNotificationsWithPushToken:(NSString *)pushToken
voipToken:(NSString *)voipToken voipToken:(NSString *)voipToken
isForcedUpdate:(BOOL)isForcedUpdate
success:(void (^)(void))successHandler success:(void (^)(void))successHandler
failure:(void (^)(NSError *))failureHandler failure:(void (^)(NSError *))failureHandler
remainingRetries:(int)remainingRetries remainingRetries:(int)remainingRetries
{ {
BOOL isUsingFullAPNs = [NSUserDefaults.standardUserDefaults boolForKey:@"isUsingFullAPNs"]; BOOL isUsingFullAPNs = [NSUserDefaults.standardUserDefaults boolForKey:@"isUsingFullAPNs"];
AnyPromise *promise = isUsingFullAPNs ? [LKPushNotificationManager registerWithToken:pushToken hexEncodedPublicKey:self.localNumber] AnyPromise *promise = isUsingFullAPNs ? [LKPushNotificationManager registerWithToken:pushToken hexEncodedPublicKey:self.localNumber isForcedUpdate:isForcedUpdate]
: [LKPushNotificationManager registerWithToken:pushToken]; : [LKPushNotificationManager registerWithToken:pushToken isForcedUpdate:isForcedUpdate];
promise promise
.then(^() { .then(^() {
successHandler(); successHandler();
}) })
.catch(^(NSError *error) { .catch(^(NSError *error) {
if (remainingRetries > 0) { if (remainingRetries > 0) {
[self registerForPushNotificationsWithPushToken:pushToken voipToken:voipToken success:successHandler failure:failureHandler [self registerForPushNotificationsWithPushToken:pushToken voipToken:voipToken isForcedUpdate:isForcedUpdate success:successHandler failure:failureHandler
remainingRetries:remainingRetries - 1]; remainingRetries:remainingRetries - 1];
} else { } else {
if (!IsNSErrorNetworkFailure(error)) { if (!IsNSErrorNetworkFailure(error)) {

@ -17,14 +17,14 @@ public final class LokiPushNotificationManager : NSObject {
// MARK: Registration // MARK: Registration
/// Registers the user for silent push notifications (that then trigger the app /// Registers the user for silent push notifications (that then trigger the app
/// into fetching messages). Only the user's device token is needed for this. /// into fetching messages). Only the user's device token is needed for this.
static func register(with token: Data) -> Promise<Void> { static func register(with token: Data, isForcedUpdate: Bool) -> Promise<Void> {
let hexEncodedToken = token.toHexString() let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard let userDefaults = UserDefaults.standard
let oldToken = userDefaults[.deviceToken] let oldToken = userDefaults[.deviceToken]
let lastUploadTime = userDefaults[.lastDeviceTokenUpload] let lastUploadTime = userDefaults[.lastDeviceTokenUpload]
let isUsingFullAPNs = userDefaults[.isUsingFullAPNs] let isUsingFullAPNs = userDefaults[.isUsingFullAPNs]
let now = Date().timeIntervalSince1970 let now = Date().timeIntervalSince1970
guard hexEncodedToken != oldToken || now - lastUploadTime < tokenExpirationInterval else { guard isForcedUpdate || hexEncodedToken != oldToken || now - lastUploadTime < tokenExpirationInterval else {
print("[Loki] Device token hasn't changed; no need to re-upload.") print("[Loki] Device token hasn't changed; no need to re-upload.")
return Promise<Void> { $0.fulfill(()) } return Promise<Void> { $0.fulfill(()) }
} }
@ -56,14 +56,14 @@ public final class LokiPushNotificationManager : NSObject {
/// Registers the user for silent push notifications (that then trigger the app /// Registers the user for silent push notifications (that then trigger the app
/// into fetching messages). Only the user's device token is needed for this. /// into fetching messages). Only the user's device token is needed for this.
@objc(registerWithToken:) @objc(registerWithToken:isForcedUpdate:)
static func objc_register(with token: Data) -> AnyPromise { static func objc_register(with token: Data, isForcedUpdate: Bool) -> AnyPromise {
return AnyPromise.from(register(with: token)) return AnyPromise.from(register(with: token, isForcedUpdate: isForcedUpdate))
} }
/// Registers the user for normal push notifications. Requires the user's device /// Registers the user for normal push notifications. Requires the user's device
/// token and their Session ID. /// token and their Session ID.
static func register(with token: Data, hexEncodedPublicKey: String) -> Promise<Void> { static func register(with token: Data, hexEncodedPublicKey: String, isForcedUpdate: Bool) -> Promise<Void> {
let hexEncodedToken = token.toHexString() let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard let userDefaults = UserDefaults.standard
let now = Date().timeIntervalSince1970 let now = Date().timeIntervalSince1970
@ -91,9 +91,9 @@ public final class LokiPushNotificationManager : NSObject {
/// Registers the user for normal push notifications. Requires the user's device /// Registers the user for normal push notifications. Requires the user's device
/// token and their Session ID. /// token and their Session ID.
@objc(registerWithToken:hexEncodedPublicKey:) @objc(registerWithToken:hexEncodedPublicKey:isForcedUpdate:)
static func objc_register(with token: Data, hexEncodedPublicKey: String) -> AnyPromise { static func objc_register(with token: Data, hexEncodedPublicKey: String, isForcedUpdate: Bool) -> AnyPromise {
return AnyPromise.from(register(with: token, hexEncodedPublicKey: hexEncodedPublicKey)) return AnyPromise.from(register(with: token, hexEncodedPublicKey: hexEncodedPublicKey, isForcedUpdate: isForcedUpdate))
} }
@objc(acknowledgeDeliveryForMessageWithHash:expiration:hexEncodedPublicKey:) @objc(acknowledgeDeliveryForMessageWithHash:expiration:hexEncodedPublicKey:)

Loading…
Cancel
Save