|
|
@ -9,13 +9,13 @@
|
|
|
|
#import "PushManager.h"
|
|
|
|
#import "PushManager.h"
|
|
|
|
#import "Environment.h"
|
|
|
|
#import "Environment.h"
|
|
|
|
#import "CallServerRequestsManager.h"
|
|
|
|
#import "CallServerRequestsManager.h"
|
|
|
|
|
|
|
|
#import "FutureUtil.h"
|
|
|
|
|
|
|
|
|
|
|
|
@interface PushManager ()
|
|
|
|
@interface PushManager ()
|
|
|
|
|
|
|
|
|
|
|
|
@property (nonatomic, copy) void (^PushRegisteringSuccessBlock)();
|
|
|
|
@property TOCFutureSource *registerWithServerFutureSource;
|
|
|
|
@property (nonatomic, copy) void (^PushRegisteringFailureBlock)();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property int retries;
|
|
|
|
@property UIAlertView *missingPermissionsAlertView;
|
|
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
@ -26,123 +26,209 @@
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
sharedManager = [self new];
|
|
|
|
sharedManager = [self new];
|
|
|
|
|
|
|
|
sharedManager.missingPermissionsAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ACTION_REQUIRED_TITLE", @"") message:NSLocalizedString(@"PUSH_SETTINGS_MESSAGE", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles:nil, nil];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return sharedManager;
|
|
|
|
return sharedManager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)verifyPushActivated{
|
|
|
|
- (void)verifyPushPermissions{
|
|
|
|
UIRemoteNotificationType notificationTypes = [UIApplication.sharedApplication enabledRemoteNotificationTypes];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOL needsPushSettingChangeAlert = NO;
|
|
|
|
if (SYSTEM_VERSION_LESS_THAN(_iOS_8_0)) {
|
|
|
|
|
|
|
|
|
|
|
|
if (notificationTypes == UIRemoteNotificationTypeNone) {
|
|
|
|
// Displaying notifications and ringing
|
|
|
|
needsPushSettingChangeAlert = YES;
|
|
|
|
|
|
|
|
} else if (notificationTypes == UIRemoteNotificationTypeBadge) {
|
|
|
|
if ([self isMissingMandatoryNotificationTypes:[UIApplication.sharedApplication enabledRemoteNotificationTypes]]) {
|
|
|
|
needsPushSettingChangeAlert = YES;
|
|
|
|
|
|
|
|
} else if (notificationTypes == UIRemoteNotificationTypeAlert) {
|
|
|
|
[self registrationWithSuccess:^{
|
|
|
|
needsPushSettingChangeAlert = YES;
|
|
|
|
DDLogInfo(@"Push notifications were succesfully re-enabled");
|
|
|
|
} else if (notificationTypes == UIRemoteNotificationTypeSound) {
|
|
|
|
} failure:^{
|
|
|
|
needsPushSettingChangeAlert = YES;
|
|
|
|
[self.missingPermissionsAlertView show];
|
|
|
|
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)) {
|
|
|
|
}];
|
|
|
|
needsPushSettingChangeAlert = YES;
|
|
|
|
}
|
|
|
|
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)) {
|
|
|
|
|
|
|
|
needsPushSettingChangeAlert = YES;
|
|
|
|
} else{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UIUserNotificationsSettings
|
|
|
|
if (needsPushSettingChangeAlert) {
|
|
|
|
UIUserNotificationSettings *settings = [UIApplication.sharedApplication currentUserNotificationSettings];
|
|
|
|
[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];
|
|
|
|
// To use Signal, it is required to have sound notifications and alert types.
|
|
|
|
[alertView show];
|
|
|
|
|
|
|
|
} else if (!needsPushSettingChangeAlert){
|
|
|
|
if ([self isMissingMandatoryNotificationTypes:settings.types]) {
|
|
|
|
if (Environment.preferences.encounteredRevokedPushPermission) {
|
|
|
|
|
|
|
|
[self askForPushRegistration];
|
|
|
|
[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.
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (void)askForPushRegistrationWithSuccess:(void (^)())success failure:(void (^)())failure{
|
|
|
|
|
|
|
|
self.PushRegisteringSuccessBlock = success;
|
|
|
|
|
|
|
|
self.PushRegisteringFailureBlock = failure;
|
|
|
|
|
|
|
|
[self askForPushRegistration];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)askForPushRegistration{
|
|
|
|
- (void)registrationWithSuccess:(void (^)())success failure:(void (^)())failure{
|
|
|
|
|
|
|
|
|
|
|
|
if(SYSTEM_VERSION_LESS_THAN(_iOS_8_0)){
|
|
|
|
if (SYSTEM_VERSION_LESS_THAN(_iOS_8_0)) {
|
|
|
|
[UIApplication.sharedApplication registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
|
|
|
|
|
|
|
|
} else{
|
|
|
|
// On iOS7, we just need to register for Push Notifications (user notifications are enabled with them)
|
|
|
|
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
|
|
|
|
[self registrationForPushWithSuccess:success failure:failure];
|
|
|
|
UIMutableUserNotificationAction *action_accept = [UIMutableUserNotificationAction new];
|
|
|
|
|
|
|
|
action_accept.identifier = @"Signal_Call_Accept";
|
|
|
|
|
|
|
|
action_accept.title = @"Pick up";
|
|
|
|
|
|
|
|
action_accept.activationMode = UIUserNotificationActivationModeForeground;
|
|
|
|
|
|
|
|
action_accept.destructive = YES;
|
|
|
|
|
|
|
|
action_accept.authenticationRequired = NO;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UIMutableUserNotificationAction *action_decline = [UIMutableUserNotificationAction new];
|
|
|
|
|
|
|
|
action_decline.identifier = @"Signal_Call_Decline";
|
|
|
|
|
|
|
|
action_decline.title = @"Pick up";
|
|
|
|
|
|
|
|
action_decline.activationMode = UIUserNotificationActivationModeForeground;
|
|
|
|
|
|
|
|
action_decline.destructive = YES;
|
|
|
|
|
|
|
|
action_decline.authenticationRequired = NO;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UIMutableUserNotificationCategory *callCategory = [UIMutableUserNotificationCategory new];
|
|
|
|
|
|
|
|
callCategory.identifier = @"Signal_IncomingCall";
|
|
|
|
|
|
|
|
[callCategory setActions:@[action_accept, action_decline] forContext:UIUserNotificationActionContextDefault];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NSSet *categories = [NSSet setWithObject:callCategory];
|
|
|
|
} else{
|
|
|
|
|
|
|
|
|
|
|
|
[UIApplication.sharedApplication registerForRemoteNotifications];
|
|
|
|
// On iOS 8+, both Push Notifications and User Notfications need to be registered.
|
|
|
|
[UIApplication.sharedApplication registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound) categories:categories]];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
[self registrationForPushWithSuccess:^{
|
|
|
|
|
|
|
|
[self registrationForUserNotificationWithSuccess:success failure:^{
|
|
|
|
|
|
|
|
[self.missingPermissionsAlertView show];
|
|
|
|
|
|
|
|
failure();
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
} failure:failure];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.retries = 3;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)registerForPushWithToken:(NSData*)token{
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark Private Methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark Register Push Notification Token with server
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-(TOCFuture*)registerForPushFutureWithToken:(NSData*)token{
|
|
|
|
|
|
|
|
self.registerWithServerFutureSource = [TOCFutureSource new];
|
|
|
|
|
|
|
|
|
|
|
|
[CallServerRequestsManager.sharedInstance registerPushToken:token success:^(NSURLSessionDataTask *task, id responseObject) {
|
|
|
|
[CallServerRequestsManager.sharedInstance registerPushToken:token success:^(NSURLSessionDataTask *task, id responseObject) {
|
|
|
|
if ([task.response isKindOfClass: NSHTTPURLResponse.class]){
|
|
|
|
if ([task.response isKindOfClass: NSHTTPURLResponse.class]){
|
|
|
|
NSInteger statusCode = [(NSHTTPURLResponse*) task.response statusCode];
|
|
|
|
NSInteger statusCode = [(NSHTTPURLResponse*) task.response statusCode];
|
|
|
|
if (statusCode == 200) {
|
|
|
|
if (statusCode == 200) {
|
|
|
|
DDLogInfo(@"Device sent push ID to server");
|
|
|
|
[self.registerWithServerFutureSource trySetResult:@YES];
|
|
|
|
[Environment.preferences setRevokedPushPermission:NO];
|
|
|
|
|
|
|
|
if (self.PushRegisteringSuccessBlock) {
|
|
|
|
|
|
|
|
self.PushRegisteringSuccessBlock();
|
|
|
|
|
|
|
|
self.PushRegisteringSuccessBlock = nil;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else{
|
|
|
|
} else{
|
|
|
|
[self registerFailureWithToken:token];
|
|
|
|
DDLogError(@"The server returned %@ instead of a 200 status code", task.response);
|
|
|
|
|
|
|
|
[self.registerWithServerFutureSource trySetFailure:@NO];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else{
|
|
|
|
|
|
|
|
[self.registerWithServerFutureSource trySetFailure:@NO];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} failure:^(NSURLSessionDataTask *task, NSError *error) {
|
|
|
|
} failure:^(NSURLSessionDataTask *task, NSError *error) {
|
|
|
|
[self registerFailureWithToken:token];
|
|
|
|
[self.registerWithServerFutureSource trySetFailure:@NO];
|
|
|
|
}];
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
return self.pushNotificationFutureSource.future;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
-(TOCFuture*)registerForUserNotificationsFuture{
|
|
|
|
* Token was not sucessfully register. Try again / deal with failure
|
|
|
|
self.userNotificationFutureSource = [TOCFutureSource new];
|
|
|
|
*
|
|
|
|
[UIApplication.sharedApplication registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationType)[self allNotificationTypes] categories:[NSSet setWithObject:[self userNotificationsCallCategory]]]];
|
|
|
|
* @param token Token to register
|
|
|
|
return self.userNotificationFutureSource.future;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)registerFailureWithToken:(NSData*)token{
|
|
|
|
- (void)registrationForPushWithSuccess:(void (^)())success failure:(void (^)())failure{
|
|
|
|
if (self.retries > 0) {
|
|
|
|
TOCFuture *requestPushTokenFuture;
|
|
|
|
[self registerForPushWithToken:token];
|
|
|
|
|
|
|
|
self.retries--;
|
|
|
|
if (SYSTEM_VERSION_LESS_THAN(_iOS_8_0)) {
|
|
|
|
|
|
|
|
requestPushTokenFuture = [self registeriOS7PushNotificationFuture];
|
|
|
|
} else{
|
|
|
|
} else{
|
|
|
|
if (self.PushRegisteringFailureBlock) {
|
|
|
|
requestPushTokenFuture = [self registerPushNotificationFuture];
|
|
|
|
self.PushRegisteringFailureBlock();
|
|
|
|
|
|
|
|
self.PushRegisteringFailureBlock = nil;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[Environment.preferences setRevokedPushPermission:YES];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[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];
|
|
|
|
|
|
|
|
[failureToRegisterWithServerAlert show];
|
|
|
|
|
|
|
|
failure();
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[registerPushTokenFuture thenDo:^(id value) {
|
|
|
|
|
|
|
|
success();
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (void)registrationForUserNotificationWithSuccess:(void (^)())success failure:(void (^)())failure{
|
|
|
|
|
|
|
|
TOCFuture *registrerUserNotificationFuture = [self registerForUserNotificationsFuture];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[registrerUserNotificationFuture catchDo:^(id failureObj) {
|
|
|
|
|
|
|
|
[self.missingPermissionsAlertView show];
|
|
|
|
|
|
|
|
failure();
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[registrerUserNotificationFuture thenDo:^(id types) {
|
|
|
|
|
|
|
|
if ([self isMissingMandatoryNotificationTypes:[UIApplication.sharedApplication currentUserNotificationSettings].types]) {
|
|
|
|
|
|
|
|
[self.missingPermissionsAlertView show];
|
|
|
|
|
|
|
|
failure();
|
|
|
|
|
|
|
|
} else{
|
|
|
|
|
|
|
|
success();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-(UIUserNotificationCategory*)userNotificationsCallCategory{
|
|
|
|
|
|
|
|
UIMutableUserNotificationAction *action_accept = [UIMutableUserNotificationAction new];
|
|
|
|
|
|
|
|
action_accept.identifier = Signal_Accept_Identifier;
|
|
|
|
|
|
|
|
action_accept.title = NSLocalizedString(@"ANSWER_CALL_BUTTON_TITLE", @"");
|
|
|
|
|
|
|
|
action_accept.activationMode = UIUserNotificationActivationModeForeground;
|
|
|
|
|
|
|
|
action_accept.destructive = NO;
|
|
|
|
|
|
|
|
action_accept.authenticationRequired = NO;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UIMutableUserNotificationAction *action_decline = [UIMutableUserNotificationAction new];
|
|
|
|
|
|
|
|
action_decline.identifier = Signal_Decline_Identifier;
|
|
|
|
|
|
|
|
action_decline.title = NSLocalizedString(@"REJECT_CALL_BUTTON_TITLE", @"");
|
|
|
|
|
|
|
|
action_decline.activationMode = UIUserNotificationActivationModeBackground;
|
|
|
|
|
|
|
|
action_decline.destructive = NO;
|
|
|
|
|
|
|
|
action_decline.authenticationRequired = NO;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UIMutableUserNotificationCategory *callCategory = [UIMutableUserNotificationCategory new];
|
|
|
|
|
|
|
|
callCategory.identifier = @"Signal_IncomingCall";
|
|
|
|
|
|
|
|
[callCategory setActions:@[action_accept, action_decline] forContext:UIUserNotificationActionContextMinimal];
|
|
|
|
|
|
|
|
[callCategory setActions:@[action_accept, action_decline] forContext:UIUserNotificationActionContextDefault];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return callCategory;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-(BOOL)isMissingMandatoryNotificationTypes:(int)notificationTypes{
|
|
|
|
|
|
|
|
int mandatoryTypes = [self mandatoryNotificationTypes];
|
|
|
|
|
|
|
|
return ((mandatoryTypes & notificationTypes) == mandatoryTypes)?NO:YES;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-(int)allNotificationTypes{
|
|
|
|
|
|
|
|
return (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-(int)mandatoryNotificationTypes{
|
|
|
|
|
|
|
|
return (UIUserNotificationTypeAlert | UIUserNotificationTypeSound);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
@end
|
|
|
|