diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index f8551ba88..8e5d02c72 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -120,6 +120,7 @@ if ([TSAccountManager isRegistered]) { [TSSocketManager becomeActive]; + [[PushManager sharedManager] validateUserNotificationSettings]; [self refreshContacts]; [TSPreKeyManager refreshPreKeys]; } diff --git a/Signal/src/network/PushManager.h b/Signal/src/network/PushManager.h index 0d08a618a..184cd6b0a 100644 --- a/Signal/src/network/PushManager.h +++ b/Signal/src/network/PushManager.h @@ -29,29 +29,32 @@ typedef void(^failedPushRegistrationBlock)(NSError *error); + (PushManager*)sharedManager; /** - * Push notification registration method + * Registers the push token with the RedPhone server, then returns the push token and a signup token to be used to register with TextSecure. * - * @param success Block to execute after succesful push notification registration - * @param failure Block to executre if push notification registration fails + * @param success Success completion block - registering with TextSecure server + * @param failure Failure completion block */ -- (void)registrationWithSuccess:(void (^)())success failure:(failedPushRegistrationBlock)failure; +- (void)registrationAndRedPhoneTokenRequestWithSuccess:(void (^)(NSData* pushToken, NSString* signupToken))success failure:(failedPushRegistrationBlock)failure; /** - * Registers the push token with the RedPhone server, then returns the push token and a signup token to be used to register with TextSecure. + * Returns the Push Notification Token of this device * - * @param success Success completion block - registering with TextSecure server - * @param failure Failure completion block + * @param success Completion block that is passed the token as a parameter + * @param failure Failure block, executed when failed to get push token */ -- (void)registrationAndRedPhoneTokenRequestWithSuccess:(void (^)(NSData* pushToken, NSString* signupToken))success failure:(failedPushRegistrationBlock)failure; +- (void)requestPushTokenWithSuccess:(void (^)(NSData* pushToken))success failure:(void(^)(NSError *))failure; /** - * The pushNotification and userNotificationFutureSource are accessed by the App Delegate after requested permissions. + * Registers for Users Notifications. By doing this on launch, we are sure that the correct categories of user notifications is registered. */ --(TOCFuture*)registerPushNotificationFuture; -- (void)registrationForPushWithSuccess:(void (^)(NSData* pushToken))success failure:(void(^)(NSError *))failure; +- (void)validateUserNotificationSettings; + +/** + * The pushNotification and userNotificationFutureSource are accessed by the App Delegate after requested permissions. + */ @property TOCFutureSource *pushNotificationFutureSource; @property TOCFutureSource *userNotificationFutureSource; diff --git a/Signal/src/network/PushManager.m b/Signal/src/network/PushManager.m index a18c166e4..49445869a 100644 --- a/Signal/src/network/PushManager.m +++ b/Signal/src/network/PushManager.m @@ -43,47 +43,6 @@ return self; } -- (void)registrationWithSuccess:(void (^)())success failure:(failedPushRegistrationBlock)failure{ - if (!self.wantRemoteNotifications) { - success(); - return; - } - - [self registrationForPushWithSuccess:^(NSData* pushToken){ - [self registrationForUserNotificationWithSuccess:success failure:^(NSError *error) { - [self.missingPermissionsAlertView show]; - failure([NSError errorWithDomain:pushManagerDomain code:400 userInfo:@{}]); - }]; - } failure:failure]; -} - -#pragma mark Private Methods - -#pragma mark Register Push Notification Token with server - --(TOCFuture*)registerForPushFutureWithToken:(NSData*)token{ - self.registerWithServerFutureSource = [TOCFutureSource new]; - - [RPServerRequestsManager.sharedInstance performRequest:[RPAPICall registerPushNotificationWithPushToken:token] success:^(NSURLSessionDataTask *task, id responseObject) { - if ([task.response isKindOfClass: NSHTTPURLResponse.class]){ - NSInteger statusCode = [(NSHTTPURLResponse*) task.response statusCode]; - if (statusCode == 200) { - [self.registerWithServerFutureSource trySetResult:@YES]; - } else{ - DDLogError(@"The server returned %@ instead of a 200 status code", task.response); - [self.registerWithServerFutureSource trySetFailure:[NSError errorWithDomain:pushManagerDomain code:500 userInfo:nil]]; - } - } else{ - [self.registerWithServerFutureSource trySetFailure:task.response]; - } - - } failure:^(NSURLSessionDataTask *task, NSError *error) { - [self.registerWithServerFutureSource trySetFailure:error]; - }]; - - return self.registerWithServerFutureSource.future; -} - #pragma mark Register device for Push Notification locally -(TOCFuture*)registerPushNotificationFuture{ @@ -93,7 +52,7 @@ return self.pushNotificationFutureSource.future; } -- (void)registrationForPushWithSuccess:(void (^)(NSData* pushToken))success failure:(failedPushRegistrationBlock)failure{ +- (void)requestPushTokenWithSuccess:(void (^)(NSData* pushToken))success failure:(failedPushRegistrationBlock)failure{ TOCFuture *requestPushTokenFuture = [self registerPushNotificationFuture]; [requestPushTokenFuture catchDo:^(id failureObj) { @@ -110,13 +69,22 @@ }]; [registerPushTokenFuture thenDo:^(id value) { - success(pushToken); + TOCFuture *userRegistration = [self registerForUserNotificationsFuture]; + + [userRegistration thenDo:^(UIUserNotificationSettings *userNotificationSettings) { + success(pushToken); + }]; }]; }]; } - (void)registrationAndRedPhoneTokenRequestWithSuccess:(void (^)(NSData* pushToken, NSString* signupToken))success failure:(failedPushRegistrationBlock)failure{ - [self registrationForPushWithSuccess:^(NSData *pushToken) { + if (!self.wantRemoteNotifications) { + success([@"Fake PushToken" dataUsingEncoding:NSUTF8StringEncoding], @""); + return; + } + + [self requestPushTokenWithSuccess:^(NSData* pushToken){ [RPServerRequestsManager.sharedInstance performRequest:[RPAPICall requestTextSecureVerificationCode] success:^(NSURLSessionDataTask *task, id responseObject) { NSError *error; @@ -132,7 +100,10 @@ } failure:^(NSURLSessionDataTask *task, NSError *error) { failure(error); }]; - } failure:failure]; + } failure:^(NSError *error) { + [self.missingPermissionsAlertView show]; + failure([NSError errorWithDomain:pushManagerDomain code:400 userInfo:@{}]); + }]; } -(TOCFuture*)registerForUserNotificationsFuture{ @@ -143,18 +114,6 @@ return self.userNotificationFutureSource.future; } -- (void)registrationForUserNotificationWithSuccess:(void (^)())success failure:(void (^)())failure{ - TOCFuture *registrerUserNotificationFuture = [self registerForUserNotificationsFuture]; - - [registrerUserNotificationFuture catchDo:^(id failureObj) { - failure(); - }]; - - [registrerUserNotificationFuture thenDo:^(id types) { - success(); - }]; -} - -(BOOL) needToRegisterForRemoteNotifications { return self.wantRemoteNotifications && (!UIApplication.sharedApplication.isRegisteredForRemoteNotifications); } @@ -174,4 +133,35 @@ return UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge; } +- (void)validateUserNotificationSettings{ + [[self registerForUserNotificationsFuture] thenDo:^(id value) { + //Nothing to do, just making sure we are registered for User Notifications. + }]; +} + +#pragma mark Register Push Notification Token with RedPhone server + +-(TOCFuture*)registerForPushFutureWithToken:(NSData*)token{ + self.registerWithServerFutureSource = [TOCFutureSource new]; + + [RPServerRequestsManager.sharedInstance performRequest:[RPAPICall registerPushNotificationWithPushToken:token] success:^(NSURLSessionDataTask *task, id responseObject) { + if ([task.response isKindOfClass: NSHTTPURLResponse.class]){ + NSInteger statusCode = [(NSHTTPURLResponse*) task.response statusCode]; + if (statusCode == 200) { + [self.registerWithServerFutureSource trySetResult:@YES]; + } else{ + DDLogError(@"The server returned %@ instead of a 200 status code", task.response); + [self.registerWithServerFutureSource trySetFailure:[NSError errorWithDomain:pushManagerDomain code:500 userInfo:nil]]; + } + } else{ + [self.registerWithServerFutureSource trySetFailure:task.response]; + } + + } failure:^(NSURLSessionDataTask *task, NSError *error) { + [self.registerWithServerFutureSource trySetFailure:error]; + }]; + + return self.registerWithServerFutureSource.future; +} + @end diff --git a/Signal/src/view controllers/SettingsTableViewController.m b/Signal/src/view controllers/SettingsTableViewController.m index 7fa5f270b..82c26db0f 100644 --- a/Signal/src/view controllers/SettingsTableViewController.m +++ b/Signal/src/view controllers/SettingsTableViewController.m @@ -193,7 +193,7 @@ typedef enum { - (void)proceedToUnregistration{ [TSAccountManager unregisterTextSecureWithSuccess:^{ - [PushManager.sharedManager registrationForPushWithSuccess:^(NSData* pushToken){ + [PushManager.sharedManager requestPushTokenWithSuccess:^(NSData* pushToken){ [[RPServerRequestsManager sharedInstance]performRequest:[RPAPICall unregisterWithPushToken:pushToken] success:^(NSURLSessionDataTask *task, id responseObject) { [Environment resetAppData]; exit(0);