From 43ca8b511bcc3582f93f0d6ccf67e0a6476e954b Mon Sep 17 00:00:00 2001 From: Frederic Jacobs Date: Tue, 7 Oct 2014 00:04:50 +0200 Subject: [PATCH] Fixing registration issues Fixes the simulator and client (when permissions disabled) registration fixes as discussed in #172 //FREEBIE --- Signal/src/network/PushManager.h | 2 +- Signal/src/network/PushManager.m | 131 ++++++++---------- .../xibs/RegisterViewController.xib | 11 +- 3 files changed, 67 insertions(+), 77 deletions(-) diff --git a/Signal/src/network/PushManager.h b/Signal/src/network/PushManager.h index aab7a0f01..306f6652e 100644 --- a/Signal/src/network/PushManager.h +++ b/Signal/src/network/PushManager.h @@ -19,7 +19,7 @@ @interface PushManager : NSObject -+ (instancetype)sharedManager; ++ (PushManager*)sharedManager; /** * Push notification token is always registered during signup. User can however revoke notifications. diff --git a/Signal/src/network/PushManager.m b/Signal/src/network/PushManager.m index 0aa0e16b6..7311ecddf 100644 --- a/Signal/src/network/PushManager.m +++ b/Signal/src/network/PushManager.m @@ -34,55 +34,26 @@ return sharedManager; } + - (void)verifyPushPermissions{ - - if (SYSTEM_VERSION_LESS_THAN(_iOS_8_0)) { - - // Displaying notifications and ringing - - if ([self isMissingMandatoryNotificationTypes:[UIApplication.sharedApplication enabledRemoteNotificationTypes]]) { - - [self registrationWithSuccess:^{ - DDLogInfo(@"Push notifications were succesfully re-enabled"); - } failure:^{ - [self.missingPermissionsAlertView show]; - }]; - } - - } else{ - - // UIUserNotificationsSettings - UIUserNotificationSettings *settings = [UIApplication.sharedApplication currentUserNotificationSettings]; - - // To use Signal, it is required to have sound notifications and alert types. - - if ([self isMissingMandatoryNotificationTypes:settings.types]) { - - [self registrationForUserNotificationWithSuccess:^{ - DDLogInfo(@"User notifications were succesfully re-enabled"); - } failure:^{ - [self.missingPermissionsAlertView show]; - }]; - - } - - // Remote Notifications - if (![UIApplication.sharedApplication isRegisteredForRemoteNotifications]) { - - [self registrationForPushWithSuccess:^{ - DDLogInfo(@"Push notification were succesfully re-enabled"); - } failure:^{ - DDLogError(@"The phone could not be re-registered for push notifications."); // Push tokens are not changing on the same phone, just user notification changes so it's not very important. - }]; - - } + if (self.isMissingMandatoryNotificationTypes || self.needToRegisterForRemoteNotifications){ + [self registrationWithSuccess:^{ + DDLogError(@"Re-enabled push succesfully"); + } failure:^{ + DDLogError(@"Failed to re-enable push."); + }]; } } - (void)registrationWithSuccess:(void (^)())success failure:(void (^)())failure{ + if (!self.wantRemoteNotifications) { + success(); + return; + } + if (SYSTEM_VERSION_LESS_THAN(_iOS_8_0)) { - + // On iOS7, we just need to register for Push Notifications (user notifications are enabled with them) [self registrationForPushWithSuccess:success failure:failure]; @@ -114,30 +85,29 @@ [self.registerWithServerFutureSource trySetResult:@YES]; } else{ DDLogError(@"The server returned %@ instead of a 200 status code", task.response); - [self.registerWithServerFutureSource trySetFailure:@NO]; + [self.registerWithServerFutureSource trySetFailure:nil]; } } else{ - [self.registerWithServerFutureSource trySetFailure:@NO]; + [self.registerWithServerFutureSource trySetFailure:task.response]; } - + } failure:^(NSURLSessionDataTask *task, NSError *error) { - [self.registerWithServerFutureSource trySetFailure:@NO]; + [self.registerWithServerFutureSource trySetFailure:error]; }]; - + return self.registerWithServerFutureSource.future; } #pragma mark Register device for Push Notification locally --(TOCFuture*)registeriOS7PushNotificationFuture{ - self.pushNotificationFutureSource = [TOCFutureSource new]; - [UIApplication.sharedApplication registerForRemoteNotificationTypes:(UIRemoteNotificationType)[self mandatoryNotificationTypes]]; - return self.pushNotificationFutureSource.future; -} - -(TOCFuture*)registerPushNotificationFuture{ self.pushNotificationFutureSource = [TOCFutureSource new]; - [[UIApplication sharedApplication] registerForRemoteNotifications]; + + if (SYSTEM_VERSION_LESS_THAN(_iOS_8_0)) { + [UIApplication.sharedApplication registerForRemoteNotificationTypes:(UIRemoteNotificationType)self.mandatoryNotificationTypes]; + } else { + [UIApplication.sharedApplication registerForRemoteNotifications]; + } return self.pushNotificationFutureSource.future; } @@ -148,28 +118,23 @@ } - (void)registrationForPushWithSuccess:(void (^)())success failure:(void (^)())failure{ - TOCFuture *requestPushTokenFuture; - - if (SYSTEM_VERSION_LESS_THAN(_iOS_8_0)) { - requestPushTokenFuture = [self registeriOS7PushNotificationFuture]; - } else{ - requestPushTokenFuture = [self registerPushNotificationFuture]; - } + TOCFuture *requestPushTokenFuture = [self registerPushNotificationFuture]; [requestPushTokenFuture catchDo:^(id failureObj) { failure(); - if (SYSTEM_VERSION_LESS_THAN(_iOS_8_0)) { - [self.missingPermissionsAlertView show]; - } else{ - DDLogError(@"This should not happen on iOS8. No push token was provided"); - } + [self.missingPermissionsAlertView show]; + DDLogError(@"This should not happen on iOS8. No push token was provided"); }]; [requestPushTokenFuture thenDo:^(NSData* pushToken) { TOCFuture *registerPushTokenFuture = [self registerForPushFutureWithToken:pushToken]; [registerPushTokenFuture catchDo:^(id failureObj) { - UIAlertView *failureToRegisterWithServerAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"REGISTRATION_ERROR", @"") message:NSLocalizedString(@"REGISTRATION_BODY", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil, nil]; + UIAlertView *failureToRegisterWithServerAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"REGISTRATION_ERROR", @"") + message:NSLocalizedString(@"REGISTRATION_BODY", nil) + delegate:nil + cancelButtonTitle:NSLocalizedString(@"OK", nil) + otherButtonTitles:nil, nil]; [failureToRegisterWithServerAlert show]; failure(); }]; @@ -189,7 +154,7 @@ }]; [registrerUserNotificationFuture thenDo:^(id types) { - if ([self isMissingMandatoryNotificationTypes:[UIApplication.sharedApplication currentUserNotificationSettings].types]) { + if (self.isMissingMandatoryNotificationTypes) { [self.missingPermissionsAlertView show]; failure(); } else{ @@ -198,6 +163,20 @@ }]; } +-(BOOL) needToRegisterForRemoteNotifications { + return self.wantRemoteNotifications && !UIApplication.sharedApplication.isRegisteredForRemoteNotifications; +} + +-(BOOL) wantRemoteNotifications { + BOOL isSimulator = [UIDevice.currentDevice.model.lowercaseString rangeOfString:@"simulator"].location != NSNotFound; + + if (isSimulator) { + // Simulator is used for debugging but can't receive push notifications, so don't bother trying to get them + return NO; + } + + return YES; +} -(UIUserNotificationCategory*)userNotificationsCallCategory{ UIMutableUserNotificationAction *action_accept = [UIMutableUserNotificationAction new]; @@ -222,17 +201,23 @@ return callCategory; } --(BOOL)isMissingMandatoryNotificationTypes:(int)notificationTypes{ - int mandatoryTypes = [self mandatoryNotificationTypes]; - return ((mandatoryTypes & notificationTypes) == mandatoryTypes)?NO:YES; +-(BOOL)isMissingMandatoryNotificationTypes { + int mandatoryTypes = self.mandatoryNotificationTypes; + int currentTypes; + if (SYSTEM_VERSION_LESS_THAN(_iOS_8_0)) { + currentTypes = UIApplication.sharedApplication.enabledRemoteNotificationTypes; + } else { + currentTypes = UIApplication.sharedApplication.currentUserNotificationSettings.types; + } + return (mandatoryTypes & currentTypes) != mandatoryTypes; } -(int)allNotificationTypes{ - return (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge); + return UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge; } -(int)mandatoryNotificationTypes{ - return (UIUserNotificationTypeAlert | UIUserNotificationTypeSound); + return UIUserNotificationTypeAlert | UIUserNotificationTypeSound; } @end diff --git a/Signal/src/view controllers/xibs/RegisterViewController.xib b/Signal/src/view controllers/xibs/RegisterViewController.xib index adc05608e..6d013eaf3 100644 --- a/Signal/src/view controllers/xibs/RegisterViewController.xib +++ b/Signal/src/view controllers/xibs/RegisterViewController.xib @@ -1,7 +1,8 @@ - + - + + @@ -278,7 +279,6 @@ ZSB0bw - @@ -286,4 +286,9 @@ ZSB0bw + + + + +