diff --git a/Riot/AppDelegate.h b/Riot/AppDelegate.h index a31983560..805f057a4 100644 --- a/Riot/AppDelegate.h +++ b/Riot/AppDelegate.h @@ -100,6 +100,15 @@ extern NSString *const AppDelegateUniversalLinkDidChangeNotification; + (AppDelegate*)theDelegate; +#pragma mark - Push Notifications + +/** + Perform registration for remote notifications. + + @param completion the block to be executed when registration finished. + */ +- (void)registerForRemoteNotificationsWithCompletion:(nullable void (^)(NSError *))completion; + #pragma mark - Application layout handling - (void)restoreInitialDisplay:(void (^)(void))completion; diff --git a/Riot/AppDelegate.m b/Riot/AppDelegate.m index feaaf897f..cd4d55cd1 100644 --- a/Riot/AppDelegate.m +++ b/Riot/AppDelegate.m @@ -210,6 +210,11 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni */ UIView *launchAnimationContainerView; NSDate *launchAnimationStart; + + /** + Related push notification service instance. Will be created when launch finished. + */ + PushNotificationService *pushNotificationService; } @property (strong, nonatomic) UIAlertController *mxInAppNotification; @@ -261,6 +266,13 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni return (AppDelegate*)[[UIApplication sharedApplication] delegate]; } +#pragma mark - Push Notifications + +- (void)registerForRemoteNotificationsWithCompletion:(nullable void (^)(NSError *))completion +{ + [pushNotificationService registerForRemoteNotificationsWithCompletion:completion]; +} + #pragma mark - - (NSString*)appVersion @@ -473,7 +485,8 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni [DecryptionFailureTracker sharedInstance].delegate = [Analytics sharedInstance]; [[Analytics sharedInstance] start]; - [PushNotificationService sharedInstance].delegate = self; + pushNotificationService = [PushNotificationService new]; + pushNotificationService.delegate = self; // Add matrix observers, and initialize matrix sessions if the app is not launched in background. [self initMatrixSessions]; @@ -590,7 +603,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. // Notify push notification service - [[PushNotificationService sharedInstance] applicationWillEnterForeground]; + [pushNotificationService applicationWillEnterForeground]; // Force each session to refresh here their publicised groups by user dictionary. // When these publicised groups are retrieved for a user, they are cached and reused until the app is backgrounded and enters in the foreground again @@ -1871,7 +1884,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni [self addMatrixCallObserver]; // Enable local notifications - [[PushNotificationService sharedInstance] enableLocalNotificationsFromMatrixSession:mxSession]; + [pushNotificationService enableLocalNotificationsFromMatrixSession:mxSession]; // Look for the account related to this session. NSArray *mxAccounts = [MXKAccountManager sharedManager].activeAccounts; @@ -1903,7 +1916,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni // Consider here the case where the app is running in background. else if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) { - [[PushNotificationService sharedInstance] handleSessionStateChangesInBackgroundFor:mxSession]; + [pushNotificationService handleSessionStateChangesInBackgroundFor:mxSession]; } else if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) { @@ -1933,7 +1946,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni // Set the push gateway URL. account.pushGatewayURL = [[NSUserDefaults standardUserDefaults] objectForKey:@"pushGatewayURL"]; - BOOL isPushRegistered = [PushNotificationService sharedInstance].isPushRegistered; + BOOL isPushRegistered = pushNotificationService.isPushRegistered; NSLog(@"[AppDelegate][Push] didAddAccountNotification: isPushRegistered: %@", @(isPushRegistered)); @@ -1945,7 +1958,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni else { // Set up push notifications - [[PushNotificationService sharedInstance] registerUserNotificationSettings]; + [pushNotificationService registerUserNotificationSettings]; } // Observe inApp notifications toggle change @@ -2052,7 +2065,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni } // Set up push notifications - [[PushNotificationService sharedInstance] registerUserNotificationSettings]; + [pushNotificationService registerUserNotificationSettings]; // Observe inApp notifications toggle change for each account for (MXKAccount *account in mxAccounts) @@ -2099,7 +2112,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni // Do the one time check on device id [self checkDeviceId:mxSession]; - [[PushNotificationService sharedInstance] addMatrixSession:mxSession]; + [pushNotificationService addMatrixSession:mxSession]; // Enable listening of incoming key share requests [self enableRoomKeyRequestObserver:mxSession]; @@ -2123,7 +2136,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni [self disableNoVoIPOnMatrixSession:mxSession]; // Disable local notifications from this session - [[PushNotificationService sharedInstance] disableLocalNotificationsFromMatrixSession:mxSession]; + [pushNotificationService disableLocalNotificationsFromMatrixSession:mxSession]; // Disable listening of incoming key share requests [self disableRoomKeyRequestObserver:mxSession]; @@ -2131,7 +2144,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni // Disable listening of incoming key verification requests [self disableIncomingKeyVerificationObserver:mxSession]; - [[PushNotificationService sharedInstance] removeMatrixSession:mxSession]; + [pushNotificationService removeMatrixSession:mxSession]; [mxSessionArray removeObject:mxSession]; @@ -2269,7 +2282,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni - (void)logoutSendingRequestServer:(BOOL)sendLogoutServerRequest completion:(void (^)(BOOL isLoggedOut))completion { - [[PushNotificationService sharedInstance] deregisterRemoteNotifications]; + [pushNotificationService deregisterRemoteNotifications]; // Clear cache [MXMediaManager clearCache]; @@ -2846,7 +2859,7 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni [self.masterTabBarController selectRoomWithId:roomId andEventId:eventId inMatrixSession:mxSession completion:^{ // Remove delivered notifications for this room - [[PushNotificationService sharedInstance] removeDeliveredNotificationsWithRoomId:roomId completion:nil]; + [pushNotificationService removeDeliveredNotificationsWithRoomId:roomId completion:nil]; if (completion) { diff --git a/Riot/Managers/PushNotification/PushNotificationService.h b/Riot/Managers/PushNotification/PushNotificationService.h index 08e96c4f8..320b81dd5 100644 --- a/Riot/Managers/PushNotification/PushNotificationService.h +++ b/Riot/Managers/PushNotification/PushNotificationService.h @@ -26,23 +26,6 @@ NS_ASSUME_NONNULL_BEGIN @interface PushNotificationService : NSObject -/** - Return the shared Push Notificatoin service. - - @return the shared Push Notificatoin service. - */ -+ (instancetype)sharedInstance; - -/** - * Unavailable initializer. Use sharedInstance. - */ -+ (instancetype)new NS_UNAVAILABLE; - -/** - * Unavailable initializer. Use sharedInstance. - */ -- (instancetype)init NS_UNAVAILABLE; - /** Is push really registered. */ diff --git a/Riot/Managers/PushNotification/PushNotificationService.m b/Riot/Managers/PushNotification/PushNotificationService.m index 47a750355..4357fb4fc 100644 --- a/Riot/Managers/PushNotification/PushNotificationService.m +++ b/Riot/Managers/PushNotification/PushNotificationService.m @@ -56,19 +56,7 @@ @implementation PushNotificationService -+ (instancetype)sharedInstance -{ - static PushNotificationService *sharedInstance = nil; - static dispatch_once_t onceToken; - - dispatch_once(&onceToken, ^{ - sharedInstance = [[PushNotificationService alloc] initPrivate]; - }); - - return sharedInstance; -} - -- (instancetype)initPrivate +- (instancetype)init { self = [super init]; if (self) { diff --git a/Riot/Modules/Settings/SettingsViewController.m b/Riot/Modules/Settings/SettingsViewController.m index d93d67843..5b32e57f7 100644 --- a/Riot/Modules/Settings/SettingsViewController.m +++ b/Riot/Modules/Settings/SettingsViewController.m @@ -25,8 +25,6 @@ #import "AppDelegate.h" #import "AvatarGenerator.h" -#import "PushNotificationService.h" - #import "BugReportViewController.h" #import "WebViewViewController.h" @@ -2860,7 +2858,7 @@ SettingsIdentityServerCoordinatorBridgePresenterDelegate> else { // Obtain device token when user has just enabled access to notifications from system settings - [[PushNotificationService sharedInstance] registerForRemoteNotificationsWithCompletion:^(NSError * error) { + [[AppDelegate theDelegate] registerForRemoteNotificationsWithCompletion:^(NSError * error) { if (error) { [(UISwitch *)sender setOn:NO animated:YES];