diff --git a/matrixConsole/APNSHandler.m b/matrixConsole/APNSHandler.m index 44710e963..ed82bec69 100644 --- a/matrixConsole/APNSHandler.m +++ b/matrixConsole/APNSHandler.m @@ -69,6 +69,8 @@ static APNSHandler *sharedHandler = nil; self.isActive = YES; } } + + [[NSNotificationCenter defaultCenter] postNotificationName:kAPNSHandlerHasBeenUpdated object:nil]; } - (BOOL)isAvailable { diff --git a/matrixConsole/AppDelegate.m b/matrixConsole/AppDelegate.m index 63345ab3c..a4c6e2fe0 100644 --- a/matrixConsole/AppDelegate.m +++ b/matrixConsole/AppDelegate.m @@ -126,7 +126,6 @@ apnsHandler.isActive = YES; } isAPNSRegistered = YES; - [[NSNotificationCenter defaultCenter] postNotificationName:kAPNSHandlerHasBeenUpdated object:nil]; } - (void)application:(UIApplication*)app didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { diff --git a/matrixConsole/AppSettings.h b/matrixConsole/AppSettings.h index d79184e96..64fdf0b02 100644 --- a/matrixConsole/AppSettings.h +++ b/matrixConsole/AppSettings.h @@ -17,7 +17,7 @@ @interface AppSettings : NSObject -@property (nonatomic) BOOL enableNotifications; +@property (nonatomic) BOOL enableInAppNotifications; @property (nonatomic) BOOL displayAllEvents; @property (nonatomic) BOOL hideUnsupportedMessages; @property (nonatomic) BOOL sortMembersUsingLastSeenTime; diff --git a/matrixConsole/AppSettings.m b/matrixConsole/AppSettings.m index b3a0af719..f605cbdaf 100644 --- a/matrixConsole/AppSettings.m +++ b/matrixConsole/AppSettings.m @@ -43,7 +43,7 @@ static AppSettings *sharedSettings = nil; } - (void)reset { - self.enableNotifications = NO; + self.enableInAppNotifications = NO; self.displayAllEvents = NO; self.hideUnsupportedMessages = NO; self.sortMembersUsingLastSeenTime = NO; @@ -51,13 +51,13 @@ static AppSettings *sharedSettings = nil; #pragma mark - -- (BOOL)enableNotifications { - return [[NSUserDefaults standardUserDefaults] boolForKey:@"enableNotifications"]; +- (BOOL)enableInAppNotifications { + return [[NSUserDefaults standardUserDefaults] boolForKey:@"enableInAppNotifications"]; } -- (void)setEnableNotifications:(BOOL)notifications { - [[MatrixHandler sharedHandler] enableEventsNotifications:notifications]; - [[NSUserDefaults standardUserDefaults] setBool:notifications forKey:@"enableNotifications"]; +- (void)setEnableInAppNotifications:(BOOL)notifications { + [[MatrixHandler sharedHandler] enableInAppNotifications:notifications]; + [[NSUserDefaults standardUserDefaults] setBool:notifications forKey:@"enableInAppNotifications"]; } - (BOOL)displayAllEvents { diff --git a/matrixConsole/MatrixHandler.h b/matrixConsole/MatrixHandler.h index 5e9073645..4cee1eb04 100644 --- a/matrixConsole/MatrixHandler.h +++ b/matrixConsole/MatrixHandler.h @@ -49,7 +49,7 @@ extern NSString *const kMatrixHandlerUnsupportedMessagePrefix; // Flush and restore Matrix data - (void)forceInitialSync; -- (void)enableEventsNotifications:(BOOL)isEnabled; +- (void)enableInAppNotifications:(BOOL)isEnabled; - (BOOL)isSupportedAttachment:(MXEvent*)event; - (BOOL)isEmote:(MXEvent*)event; diff --git a/matrixConsole/MatrixHandler.m b/matrixConsole/MatrixHandler.m index 2325469ef..15ad2eea4 100644 --- a/matrixConsole/MatrixHandler.m +++ b/matrixConsole/MatrixHandler.m @@ -146,8 +146,8 @@ static MatrixHandler *sharedHandler = nil; }]; // Check whether the app user wants notifications on new events - if ([[AppSettings sharedSettings] enableNotifications]) { - [self enableEventsNotifications:YES]; + if ([[AppSettings sharedSettings] enableInAppNotifications]) { + [self enableInAppNotifications:YES]; } } failure:^(NSError *error) { NSLog(@"Initial Sync failed: %@", error); @@ -276,7 +276,7 @@ static MatrixHandler *sharedHandler = nil; } } -- (void)enableEventsNotifications:(BOOL)isEnabled { +- (void)enableInAppNotifications:(BOOL)isEnabled { if (isEnabled) { // Register events listener eventsListener = [self.mxSession listenToEventsOfTypes:self.eventsFilterForMessages onEvent:^(MXEvent *event, MXEventDirection direction, id customObject) { diff --git a/matrixConsole/ViewController/SettingsViewController.m b/matrixConsole/ViewController/SettingsViewController.m index 7bbe3d79c..db952806a 100644 --- a/matrixConsole/ViewController/SettingsViewController.m +++ b/matrixConsole/ViewController/SettingsViewController.m @@ -18,6 +18,7 @@ #import "AppDelegate.h" #import "AppSettings.h" +#import "APNSHandler.h" #import "MatrixHandler.h" #import "MediaManager.h" @@ -44,7 +45,8 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl NSMutableArray *errorAlerts; UIButton *logoutBtn; - UISwitch *notificationsSwitch; + UISwitch *apnsNotificationsSwitch; + UISwitch *inAppNotificationsSwitch; UISwitch *allEventsSwitch; UISwitch *unsupportedMsgSwitch; UISwitch *sortMembersSwitch; @@ -93,7 +95,8 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl errorAlerts = nil; logoutBtn = nil; - notificationsSwitch = nil; + apnsNotificationsSwitch = nil; + inAppNotificationsSwitch = nil; allEventsSwitch = nil; unsupportedMsgSwitch = nil; sortMembersSwitch = nil; @@ -106,15 +109,23 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl // Refresh display [self configureView]; [[MatrixHandler sharedHandler] addObserver:self forKeyPath:@"isResumeDone" options:0 context:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAPNSHandlerHasBeenUpdated) name:kAPNSHandlerHasBeenUpdated object:nil]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[MatrixHandler sharedHandler] removeObserver:self forKeyPath:@"isResumeDone"]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:kAPNSHandlerHasBeenUpdated object:nil]; } #pragma mark - Internal methods +- (void)onAPNSHandlerHasBeenUpdated { + // Force table reload to update notifications section + apnsNotificationsSwitch = nil; + [self.tableView reloadData]; +} + - (void)reset { // Cancel picture loader (if any) if (imageLoader) { @@ -345,8 +356,10 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl [[AppDelegate theDelegate].masterTabBarController presentMediaPicker:mediaPicker]; } else if (sender == logoutBtn) { [[AppDelegate theDelegate] logout]; - } else if (sender == notificationsSwitch) { - [AppSettings sharedSettings].enableNotifications = notificationsSwitch.on; + } else if (sender == apnsNotificationsSwitch) { + [APNSHandler sharedHandler].isActive = apnsNotificationsSwitch.on; + } else if (sender == inAppNotificationsSwitch) { + [AppSettings sharedSettings].enableInAppNotifications = inAppNotificationsSwitch.on; } else if (sender == allEventsSwitch) { [AppSettings sharedSettings].displayAllEvents = allEventsSwitch.on; } else if (sender == unsupportedMsgSwitch) { @@ -383,6 +396,9 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == SETTINGS_SECTION_NOTIFICATIONS_INDEX) { + if ([APNSHandler sharedHandler].isAvailable) { + return 2; + } return 1; } else if (section == SETTINGS_SECTION_ROOMS_INDEX) { return 3; @@ -418,17 +434,14 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl return 44; } -- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section -{ +- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 30; } -- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section -{ +- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 1; } -- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section -{ +- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UILabel *sectionHeader = [[UILabel alloc] initWithFrame:[tableView rectForHeaderInSection:section]]; sectionHeader.font = [UIFont boldSystemFontOfSize:16]; sectionHeader.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]; @@ -452,9 +465,15 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl if (indexPath.section == SETTINGS_SECTION_NOTIFICATIONS_INDEX) { SettingsTableCellWithSwitch *notificationsCell = [tableView dequeueReusableCellWithIdentifier:@"SettingsCellWithSwitch" forIndexPath:indexPath]; - notificationsCell.settingLabel.text = @"Enable notifications"; - notificationsCell.settingSwitch.on = [[AppSettings sharedSettings] enableNotifications]; - notificationsSwitch = notificationsCell.settingSwitch; + if (indexPath.row == 0) { + notificationsCell.settingLabel.text = @"Enable In-App notifications"; + notificationsCell.settingSwitch.on = [[AppSettings sharedSettings] enableInAppNotifications]; + inAppNotificationsSwitch = notificationsCell.settingSwitch; + } else { + notificationsCell.settingLabel.text = @"Enable push notifications"; + notificationsCell.settingSwitch.on = [[APNSHandler sharedHandler] isActive]; + apnsNotificationsSwitch = notificationsCell.settingSwitch; + } cell = notificationsCell; } else if (indexPath.section == SETTINGS_SECTION_ROOMS_INDEX) { SettingsTableCellWithSwitch *roomsSettingCell = [tableView dequeueReusableCellWithIdentifier:@"SettingsCellWithSwitch" forIndexPath:indexPath];