Skip to content

Commit

Permalink
Check the push notifications are enabled on app launch. Replace the n…
Browse files Browse the repository at this point in the history
…otifications toggle with a link to the settings app.
  • Loading branch information
pixlwave committed Jul 16, 2021
1 parent 2fb4bd3 commit ceb5a35
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 84 deletions.
7 changes: 7 additions & 0 deletions Riot/Managers/PushNotification/PushNotificationService.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ NS_ASSUME_NONNULL_BEGIN

- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

/**
If the user has authorized the app for push notifications, checks each account to see
if push notifications are enabled and if not, enables them. The user can opt out of push
notifications by disabling them for the app in the Settings app.
*/
- (void)ensurePushNotificationsAreEnabled;

- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;

Expand Down
40 changes: 40 additions & 0 deletions Riot/Managers/PushNotification/PushNotificationService.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,46 @@ - (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
}
}

- (void)ensurePushNotificationsAreEnabled
{
// Get the user's notification settings to check their authorization status.
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
// If the user has disabled notifications in the Settings app there is nothing to do.
if (settings.authorizationStatus == UNAuthorizationStatusDenied)
{
return;
}

// Enable notifications for all accounts in the manager
MXKAccountManager *accountManager = [MXKAccountManager sharedManager];

for (MXKAccount *account in accountManager.accounts)
{
if (!account.pushNotificationServiceIsActive)
{
if (accountManager.apnsDeviceToken)
{
[account enablePushNotifications:YES success:nil failure:nil];
}
else
{
// Obtain device token when user has just enabled access to notifications from system settings
[self registerForRemoteNotificationsWithCompletion:^(NSError * error) {
if (error)
{
MXLogError(@"[LegacyAppDelegate]: Error registering for remote notifications.");
}
else
{
[account enablePushNotifications:YES success:nil failure:nil];
}
}];
}
}
}
}];
}

- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
Expand Down
3 changes: 2 additions & 1 deletion Riot/Modules/Application/LegacyAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -1949,8 +1949,9 @@ - (void)initMatrixSessions
}
}

// Set up push notifications
// Set up push notifications and enable if authorization has been granted
[self.pushNotificationService registerUserNotificationSettings];
[self.pushNotificationService ensurePushNotificationsAreEnabled];

// Observe inApp notifications toggle change for each account
for (MXKAccount *account in mxAccounts)
Expand Down
109 changes: 26 additions & 83 deletions Riot/Modules/Settings/SettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1786,15 +1786,30 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
{
if (row == NOTIFICATION_SETTINGS_ENABLE_PUSH_INDEX)
{
MXKTableViewCellWithLabelAndSwitch* labelAndSwitchCell = [self getLabelAndSwitchCell:tableView forIndexPath:indexPath];

labelAndSwitchCell.mxkLabel.text = NSLocalizedStringFromTable(@"settings_enable_push_notif", @"Vector", nil);
labelAndSwitchCell.mxkSwitch.on = account.pushNotificationServiceIsActive;
labelAndSwitchCell.mxkSwitch.onTintColor = ThemeService.shared.theme.tintColor;
labelAndSwitchCell.mxkSwitch.enabled = YES;
[labelAndSwitchCell.mxkSwitch addTarget:self action:@selector(togglePushNotifications:) forControlEvents:UIControlEventTouchUpInside];
MXKTableViewCellWithButton *notificationsCell = [tableView dequeueReusableCellWithIdentifier:[MXKTableViewCellWithButton defaultReuseIdentifier]];
if (!notificationsCell)
{
notificationsCell = [[MXKTableViewCellWithButton alloc] init];
}
else
{
// Fix https:/vector-im/riot-ios/issues/1354
// Do not move this line in prepareForReuse because of https:/vector-im/riot-ios/issues/1323
notificationsCell.mxkButton.titleLabel.text = nil;
}

cell = labelAndSwitchCell;
NSString* title = NSLocalizedStringFromTable(@"settings_enable_push_notif", @"Vector", nil);

[notificationsCell.mxkButton setTitle:title forState:UIControlStateNormal];
[notificationsCell.mxkButton setTitle:title forState:UIControlStateHighlighted];
[notificationsCell.mxkButton setTintColor:ThemeService.shared.theme.tintColor];
notificationsCell.mxkButton.titleLabel.font = [UIFont systemFontOfSize:17];

[notificationsCell.mxkButton removeTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[notificationsCell.mxkButton addTarget:self action:@selector(openSystemSettingsApp:) forControlEvents:UIControlEventTouchUpInside];
notificationsCell.mxkButton.accessibilityIdentifier=@"SettingsVCSignOutButton"; // FIXME: Implement accessibility

cell = notificationsCell;
}
else if (row == NOTIFICATION_SETTINGS_SHOW_DECODED_CONTENT)
{
Expand Down Expand Up @@ -2789,82 +2804,10 @@ - (void)onRemove3PID:(NSIndexPath*)indexPath
}
}

- (void)togglePushNotifications:(id)sender
{
// Get the user's notification settings to check their authorization status.
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
dispatch_async(dispatch_get_main_queue(), ^{
[self togglePushNotifications:sender withNotificationSettings:settings];
});
}];
}

- (void)togglePushNotifications:(id)sender withNotificationSettings:(UNNotificationSettings * _Nonnull)settings
- (void)openSystemSettingsApp:(id)sender
{
// Check first whether the user allow notification from device settings
if (settings.authorizationStatus == UNAuthorizationStatusDenied)
{
[currentAlert dismissViewControllerAnimated:NO completion:nil];

__weak typeof(self) weakSelf = self;

NSString *appDisplayName = [[NSBundle mainBundle] infoDictionary][@"CFBundleDisplayName"];

currentAlert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:NSLocalizedStringFromTable(@"settings_on_denied_notification", @"Vector", nil), appDisplayName] message:nil preferredStyle:UIAlertControllerStyleAlert];

[currentAlert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"ok"]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {

if (weakSelf)
{
typeof(self) self = weakSelf;
self->currentAlert = nil;
}

}]];

[currentAlert mxk_setAccessibilityIdentifier: @"SettingsVCPushNotificationsAlert"];
[self presentViewController:currentAlert animated:YES completion:nil];

// Keep off the switch
((UISwitch*)sender).on = NO;
}
else if ([MXKAccountManager sharedManager].activeAccounts.count)
{
[self startActivityIndicator];

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

if (accountManager.apnsDeviceToken)
{
[account enablePushNotifications:!account.pushNotificationServiceIsActive success:^{
[self stopActivityIndicator];
} failure:^(NSError *error) {
[self stopActivityIndicator];
}];
}
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 enablePushNotifications:YES success:^{
[self stopActivityIndicator];
} failure:^(NSError *error) {
[self stopActivityIndicator];
}];
}
}];
}
}
NSURL *settingsAppURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsAppURL options:@{} completionHandler:nil];
}

- (void)toggleCallKit:(id)sender
Expand Down

0 comments on commit ceb5a35

Please sign in to comment.