mirror of https://github.com/oxen-io/session-ios
				
				
				
			- Use of new Keychain Access attribute - Error management (notifying the user) when opening app without required push and microphone permission - Enforcing APNID are sent to server, retry later if not able to reach serverpull/1/head
							parent
							
								
									5812f80d42
								
							
						
					
					
						commit
						46b869628e
					
				| @ -0,0 +1,23 @@ | |||||||
|  | //
 | ||||||
|  | //  PushManager.h
 | ||||||
|  | //  Signal
 | ||||||
|  | //
 | ||||||
|  | //  Created by Frederic Jacobs on 31/07/14.
 | ||||||
|  | //  Copyright (c) 2014 Open Whisper Systems. All rights reserved.
 | ||||||
|  | //
 | ||||||
|  | 
 | ||||||
|  | #import <Foundation/Foundation.h> | ||||||
|  | 
 | ||||||
|  | @interface PushManager : NSObject | ||||||
|  | 
 | ||||||
|  | + (instancetype)sharedManager; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | - (void)verifyPushActivated; | ||||||
|  | 
 | ||||||
|  | - (void)askForPushRegistration; | ||||||
|  | 
 | ||||||
|  | - (void)registerForPushWithToken:(NSData*)token; | ||||||
|  | 
 | ||||||
|  | @end | ||||||
|  | 
 | ||||||
| @ -0,0 +1,88 @@ | |||||||
|  | // | ||||||
|  | //  PushManager.m | ||||||
|  | //  Signal | ||||||
|  | // | ||||||
|  | //  Created by Frederic Jacobs on 31/07/14. | ||||||
|  | //  Copyright (c) 2014 Open Whisper Systems. All rights reserved. | ||||||
|  | // | ||||||
|  | #import "PreferencesUtil.h" | ||||||
|  | #import "PushManager.h" | ||||||
|  | #import "Environment.h" | ||||||
|  | #import "CallServerRequestsManager.h" | ||||||
|  | 
 | ||||||
|  | @interface PushManager () | ||||||
|  | 
 | ||||||
|  | @property int retries; | ||||||
|  | 
 | ||||||
|  | @end | ||||||
|  | 
 | ||||||
|  | @implementation PushManager | ||||||
|  | 
 | ||||||
|  | + (instancetype)sharedManager { | ||||||
|  |     static PushManager *sharedManager = nil; | ||||||
|  |     static dispatch_once_t onceToken; | ||||||
|  |     dispatch_once(&onceToken, ^{ | ||||||
|  |         sharedManager = [[self alloc] init]; | ||||||
|  |     }); | ||||||
|  |     return sharedManager; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | - (void)verifyPushActivated{ | ||||||
|  |     UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; | ||||||
|  |      | ||||||
|  |     BOOL needsPushSettingChangeAlert = NO; | ||||||
|  |      | ||||||
|  |     // enhancement: do custom message depending on the setting? | ||||||
|  |      | ||||||
|  |     if (notificationTypes == UIRemoteNotificationTypeNone) { | ||||||
|  |         needsPushSettingChangeAlert = YES; | ||||||
|  |     } else if (notificationTypes == UIRemoteNotificationTypeBadge) { | ||||||
|  |         needsPushSettingChangeAlert = YES; | ||||||
|  |     } else if (notificationTypes == UIRemoteNotificationTypeAlert) { | ||||||
|  |         needsPushSettingChangeAlert = YES; | ||||||
|  |     } else if (notificationTypes == UIRemoteNotificationTypeSound) { | ||||||
|  |         needsPushSettingChangeAlert = YES; | ||||||
|  |     } else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)) { | ||||||
|  |         needsPushSettingChangeAlert = YES; | ||||||
|  |     } else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)) { | ||||||
|  |         needsPushSettingChangeAlert = YES; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     if (needsPushSettingChangeAlert) { | ||||||
|  |         [[Environment preferences] setRevokedPushPermission:YES]; | ||||||
|  |         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ACTION_REQUIRED_TITLE", @"")  message:NSLocalizedString(@"PUSH_SETTINGS_MESSAGE", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles:nil, nil]; | ||||||
|  |         [alertView show]; | ||||||
|  |     } else if (!needsPushSettingChangeAlert){ | ||||||
|  |         if ([[Environment preferences] encounteredRevokedPushPermission]) { | ||||||
|  |             [self askForPushRegistration]; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | - (void)askForPushRegistration{ | ||||||
|  |     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)]; | ||||||
|  |     self.retries = 3; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | - (void)registerForPushWithToken:(NSData*)token{ | ||||||
|  |     [[CallServerRequestsManager sharedManager] registerPushToken:token success:^(NSURLSessionDataTask *task, id responseObject) { | ||||||
|  |         if ([task.response isKindOfClass: [NSHTTPURLResponse class]]){ | ||||||
|  |             NSInteger statusCode = [(NSHTTPURLResponse*) task.response statusCode]; | ||||||
|  |             if (statusCode == 200) { | ||||||
|  |                 DDLogInfo(@"Device sent push ID to server"); | ||||||
|  |                 [[Environment preferences] setRevokedPushPermission:NO]; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } failure:^(NSURLSessionDataTask *task, NSError *error) { | ||||||
|  |         if (self.retries > 0) { | ||||||
|  |             [self registerForPushWithToken:token]; | ||||||
|  |             self.retries--; | ||||||
|  |         } else{ | ||||||
|  |             [[Environment preferences] setRevokedPushPermission:YES]; | ||||||
|  |         } | ||||||
|  |     }]; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @end | ||||||
| @ -0,0 +1,18 @@ | |||||||
|  | //
 | ||||||
|  | //  CallServerRequests.h
 | ||||||
|  | //  Signal
 | ||||||
|  | //
 | ||||||
|  | //  Created by Frederic Jacobs on 31/07/14.
 | ||||||
|  | //  Copyright (c) 2014 Open Whisper Systems. All rights reserved.
 | ||||||
|  | //
 | ||||||
|  | 
 | ||||||
|  | #import <Foundation/Foundation.h> | ||||||
|  | #import <AFNetworking/AFNetworking.h> | ||||||
|  | 
 | ||||||
|  | @interface CallServerRequestsManager : NSObject | ||||||
|  | 
 | ||||||
|  | + (instancetype)sharedManager; | ||||||
|  | 
 | ||||||
|  | - (void)registerPushToken:(NSData*)deviceToken success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure; | ||||||
|  | 
 | ||||||
|  | @end | ||||||
| @ -0,0 +1,63 @@ | |||||||
|  | // | ||||||
|  | //  CallServerRequests.m | ||||||
|  | //  Signal | ||||||
|  | // | ||||||
|  | //  Created by Frederic Jacobs on 31/07/14. | ||||||
|  | //  Copyright (c) 2014 Open Whisper Systems. All rights reserved. | ||||||
|  | // | ||||||
|  | #import "HttpRequest.h" | ||||||
|  | #import "CallServerRequestsManager.h" | ||||||
|  | #import "DataUtil.h" | ||||||
|  | #import "Environment.h" | ||||||
|  | #import "HostNameEndPoint.h" | ||||||
|  | #import "SGNKeychainUtil.h" | ||||||
|  | 
 | ||||||
|  | #define defaultRequestTimeout | ||||||
|  | 
 | ||||||
|  | @interface CallServerRequestsManager () | ||||||
|  | 
 | ||||||
|  | @property (nonatomic, retain)AFHTTPSessionManager *operationManager; | ||||||
|  | 
 | ||||||
|  | @end | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @implementation CallServerRequestsManager | ||||||
|  | 
 | ||||||
|  | + (instancetype)sharedManager { | ||||||
|  |     static CallServerRequestsManager *sharedManager = nil; | ||||||
|  |     static dispatch_once_t onceToken; | ||||||
|  |     dispatch_once(&onceToken, ^{ | ||||||
|  |         sharedManager = [[self alloc] init]; | ||||||
|  |     }); | ||||||
|  |     return sharedManager; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | - (id)init{ | ||||||
|  |     self = [super init]; | ||||||
|  |      | ||||||
|  |     if (self) { | ||||||
|  |         HostNameEndPoint *endpoint = [[[Environment getCurrent]masterServerSecureEndPoint] hostNameEndPoint]; | ||||||
|  |         NSURL *endPointURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@:%hu", endpoint.hostname, endpoint.port]]; | ||||||
|  |         NSURLSessionConfiguration *sessionConf = [NSURLSessionConfiguration ephemeralSessionConfiguration]; | ||||||
|  |         self.operationManager = [[AFHTTPSessionManager alloc] initWithBaseURL:endPointURL sessionConfiguration:sessionConf]; | ||||||
|  |         [self.operationManager setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]]; | ||||||
|  |         self.operationManager.securityPolicy.allowInvalidCertificates = YES; // We use a custom certificate, not signed by a CA. | ||||||
|  |         self.operationManager.responseSerializer                      = [AFJSONResponseSerializer serializer]; | ||||||
|  |     } | ||||||
|  |     return self; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | - (void)registerPushToken:(NSData*)deviceToken success:(void (^)(NSURLSessionDataTask *task, id responseObject))success | ||||||
|  | failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{ | ||||||
|  |     self.operationManager.requestSerializer = [self basicAuthenticationSerializer]; | ||||||
|  |      | ||||||
|  |     [self.operationManager PUT:[NSString stringWithFormat:@"/apn/%@",[deviceToken encodedAsHexString]] parameters:@{} success:success failure:failure]; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | - (AFHTTPRequestSerializer*)basicAuthenticationSerializer{ | ||||||
|  |     AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer]; | ||||||
|  |     [serializer setValue:[HttpRequest computeBasicAuthorizationTokenForLocalNumber:[SGNKeychainUtil localNumber]andPassword:[SGNKeychainUtil serverAuthPassword]] forHTTPHeaderField:@"Authorization"]; | ||||||
|  |     return serializer; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | @end | ||||||
					Loading…
					
					
				
		Reference in New Issue