From 71e18cf903886cf4cf1f2f8b631367efeac732c8 Mon Sep 17 00:00:00 2001 From: Denis Morozov Date: Sun, 10 Sep 2017 16:41:50 +0300 Subject: [PATCH] Fix push registration process --- Riot/AppDelegate.m | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Riot/AppDelegate.m b/Riot/AppDelegate.m index bd8e9698f..52efa8f96 100644 --- a/Riot/AppDelegate.m +++ b/Riot/AppDelegate.m @@ -972,19 +972,27 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN - (void)registerForRemoteNotificationsWithCompletion:(nullable void (^)(NSError *))completion { self.registrationForRemoteNotificationsCompletion = completion; - [[UIApplication sharedApplication] registerForRemoteNotifications]; -} - -- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings -{ - if (notificationSettings.types == UIUserNotificationTypeNone) - return; self.pushRegistry = [[PKPushRegistry alloc] initWithQueue:nil]; self.pushRegistry.delegate = self; self.pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; } +- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings +{ + // Register for remote notifications only if user provide access to notification feature + if (notificationSettings.types != UIUserNotificationTypeNone) + { + [self registerForRemoteNotificationsWithCompletion:nil]; + } + else + { + // Clear existing token + MXKAccountManager* accountManager = [MXKAccountManager sharedManager]; + [accountManager setApnsDeviceToken:nil]; + } +} + - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { NSString* roomId = notification.userInfo[@"room_id"]; @@ -1039,6 +1047,12 @@ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @"kAppDelegateN [accountManager setApnsDeviceToken:token]; isAPNSRegistered = YES; + + if (self.registrationForRemoteNotificationsCompletion) + { + self.registrationForRemoteNotificationsCompletion(nil); + self.registrationForRemoteNotificationsCompletion = nil; + } } - (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(PKPushType)type