Fix APNs token updating bug

This commit is contained in:
nielsandriesse 2020-04-17 13:55:24 +10:00
parent afd560d906
commit fc9a678181
9 changed files with 33 additions and 22 deletions

View File

@ -590,9 +590,9 @@ static BOOL isInternalTestVersion = NO;
OWSLogInfo(@"Registering for push notifications with token: %@.", deviceToken);
BOOL isUsingFullAPNs = [NSUserDefaults.standardUserDefaults boolForKey:@"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 {
__unused AnyPromise *promise = [LKPushNotificationManager registerWithToken:deviceToken];
__unused AnyPromise *promise = [LKPushNotificationManager registerWithToken:deviceToken isForcedUpdate:NO];
}
}

View File

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

View File

@ -76,7 +76,9 @@ final class PNModeSheet : Sheet, OptionViewDelegate {
return present(alert, animated: true, completion: nil)
}
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()
}

View File

@ -98,6 +98,8 @@ final class PNModeVC : BaseVC, OptionViewDelegate {
TSAccountManager.sharedInstance().didRegister()
let homeVC = HomeVC()
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()
}
}

View File

@ -105,12 +105,13 @@ public class AccountManager: NSObject {
// 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
tsAccountManager.registerForPushNotifications(pushToken: pushToken,
voipToken: voipToken,
success: { resolver.fulfill(()) },
failure: resolver.reject)
isForcedUpdate: isForcedUpdate,
success: { resolver.fulfill(()) },
failure: resolver.reject)
}
}

View File

@ -137,7 +137,9 @@
- (void)didToggleAPNsSwitch:(UISwitch *)sender
{
[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

View File

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

View File

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

View File

@ -17,14 +17,14 @@ public final class LokiPushNotificationManager : NSObject {
// MARK: Registration
/// 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.
static func register(with token: Data) -> Promise<Void> {
static func register(with token: Data, isForcedUpdate: Bool) -> Promise<Void> {
let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard
let oldToken = userDefaults[.deviceToken]
let lastUploadTime = userDefaults[.lastDeviceTokenUpload]
let isUsingFullAPNs = userDefaults[.isUsingFullAPNs]
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.")
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
/// into fetching messages). Only the user's device token is needed for this.
@objc(registerWithToken:)
static func objc_register(with token: Data) -> AnyPromise {
return AnyPromise.from(register(with: token))
@objc(registerWithToken:isForcedUpdate:)
static func objc_register(with token: Data, isForcedUpdate: Bool) -> AnyPromise {
return AnyPromise.from(register(with: token, isForcedUpdate: isForcedUpdate))
}
/// Registers the user for normal push notifications. Requires the user's device
/// 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 userDefaults = UserDefaults.standard
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
/// token and their Session ID.
@objc(registerWithToken:hexEncodedPublicKey:)
static func objc_register(with token: Data, hexEncodedPublicKey: String) -> AnyPromise {
return AnyPromise.from(register(with: token, hexEncodedPublicKey: hexEncodedPublicKey))
@objc(registerWithToken:hexEncodedPublicKey:isForcedUpdate:)
static func objc_register(with token: Data, hexEncodedPublicKey: String, isForcedUpdate: Bool) -> AnyPromise {
return AnyPromise.from(register(with: token, hexEncodedPublicKey: hexEncodedPublicKey, isForcedUpdate: isForcedUpdate))
}
@objc(acknowledgeDeliveryForMessageWithHash:expiration:hexEncodedPublicKey:)