Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements in notification registration flow #1472

Merged
merged 1 commit into from
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Riot/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ extern NSString *const kAppDelegateNetworkStatusDidChangeNotification;

- (void)registerUserNotificationSettings;

/**
Perform registration for remote notifications.

@param completion the block to be executed when registration finished.
*/
- (void)registerForRemoteNotificationsWithCompletion:(void (^)(NSError *))completion;

#pragma mark - Matrix Room handling

- (void)showRoom:(NSString*)roomId andEventId:(NSString*)eventId withMatrixSession:(MXSession*)mxSession;
Expand Down
28 changes: 27 additions & 1 deletion Riot/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ The room id of the current handled remote notification (if any)
@property (strong, nonatomic) UIAlertController *mxInAppNotification;
@property (strong, nonatomic) UIAlertController *incomingCallNotification;

@property (nonatomic, nullable, copy) void (^registrationForRemoteNotificationsCompletion)(NSError *);

@end

@implementation AppDelegate
Expand Down Expand Up @@ -897,12 +899,24 @@ - (void)registerUserNotificationSettings
}
}

- (void)registerForRemoteNotificationsWithCompletion:(nullable void (^)(NSError *))completion
{
self.registrationForRemoteNotificationsCompletion = completion;
[[UIApplication sharedApplication] registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
// Register for remote notifications only if user provide access to notification feature
if (notificationSettings.types != UIUserNotificationTypeNone)
{
[application registerForRemoteNotifications];
[self registerForRemoteNotificationsWithCompletion:nil];
}
else
{
// Clear existing token
MXKAccountManager* accountManager = [MXKAccountManager sharedManager];
[accountManager setApnsDeviceToken:nil];
}
}

Expand All @@ -915,11 +929,23 @@ - (void)application:(UIApplication*)app didRegisterForRemoteNotificationsWithDev
[accountManager setApnsDeviceToken:deviceToken];

isAPNSRegistered = YES;

if (self.registrationForRemoteNotificationsCompletion)
{
self.registrationForRemoteNotificationsCompletion(nil);
self.registrationForRemoteNotificationsCompletion = nil;
}
}

- (void)application:(UIApplication*)app didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"[AppDelegate] Failed to register for APNS: %@", error);

if (self.registrationForRemoteNotificationsCompletion)
{
self.registrationForRemoteNotificationsCompletion(error);
self.registrationForRemoteNotificationsCompletion = nil;
}
}

- (void)cancelBackgroundSync
Expand Down
27 changes: 23 additions & 4 deletions Riot/ViewController/SettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2562,7 +2562,8 @@ - (void)onRemove3PID:(NSIndexPath*)path
- (void)togglePushNotifications:(id)sender
{
// Check first whether the user allow notification from device settings
if ([[MXKAccountManager sharedManager] isAPNSAvailable] == NO)
UIUserNotificationType currentUserNotificationTypes = UIApplication.sharedApplication.currentUserNotificationSettings.types;
if (currentUserNotificationTypes == UIUserNotificationTypeNone)
{
[currentAlert dismissViewControllerAnimated:NO completion:nil];

Expand Down Expand Up @@ -2594,10 +2595,28 @@ - (void)togglePushNotifications:(id)sender
{
[self startActivityIndicator];

MXKAccount* account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
MXKAccountManager *accountManager = [MXKAccountManager sharedManager];
MXKAccount* account = accountManager.activeAccounts.firstObject;

// toggle the pushes
[account setEnablePushNotifications:!account.pushNotificationServiceIsActive];
if (accountManager.apnsDeviceToken)
{
[account setEnablePushNotifications:!account.pushNotificationServiceIsActive];
}
else
{
// Obtain device token when user has just enabled access to notifications from system settings
[[AppDelegate theDelegate] registerForRemoteNotificationsWithCompletion:^(NSError * error) {
if (error)
{
[(UISwitch *)sender setOn:NO animated:YES];
[self stopActivityIndicator];
}
else
{
[account setEnablePushNotifications:YES];
}
}];
}
}
}

Expand Down