Fixing registration issues

Fixes the simulator and client (when permissions disabled) registration
fixes as discussed in #172

//FREEBIE
pull/1/head
Frederic Jacobs 11 years ago
parent 953d4d80fd
commit 43ca8b511b

@ -19,7 +19,7 @@
@interface PushManager : NSObject
+ (instancetype)sharedManager;
+ (PushManager*)sharedManager;
/**
* Push notification token is always registered during signup. User can however revoke notifications.

@ -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

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="5056" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6249" systemVersion="14A379a" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6243"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="RegisterViewController">
@ -278,7 +279,6 @@ ZSB0bw
</scrollView>
</subviews>
<color key="backgroundColor" red="0.13725490200000001" green="0.1215686275" blue="0.12549019610000001" alpha="1" colorSpace="calibratedRGB"/>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
<simulatedScreenMetrics key="simulatedDestinationMetrics"/>
</view>
</objects>
@ -286,4 +286,9 @@ ZSB0bw
<image name="drop_down_arrow_icon.png" width="14" height="9"/>
<image name="send_code_icon.png" width="12" height="12"/>
</resources>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
</document>

Loading…
Cancel
Save