From cbeaea01e6a4b822cd7dfc7b8d8d702be5056189 Mon Sep 17 00:00:00 2001 From: Marat Al Date: Thu, 25 Jul 2024 17:48:02 +0200 Subject: [PATCH] Removed unnecessary push token handling. Token is requested by ably-cocoa and received by flutter, which sends it to ably via the first available `ARTRealtime` object. All instances have the same value for the `device.push` property, since the `device` itself is stored in a static variable. --- ios/Classes/AblyFlutter.m | 29 ++--------------------------- ios/Classes/AblyInstanceStore.h | 6 ++---- ios/Classes/AblyInstanceStore.m | 21 +++++++++++---------- 3 files changed, 15 insertions(+), 41 deletions(-) diff --git a/ios/Classes/AblyFlutter.m b/ios/Classes/AblyFlutter.m index d02602b20..cdbdfcc78 100644 --- a/ios/Classes/AblyFlutter.m +++ b/ios/Classes/AblyFlutter.m @@ -76,14 +76,6 @@ -(void)reset; ARTRest *const rest = [[ARTRest alloc] initWithOptions:options.clientOptions]; [instanceStore setRest:rest with: handle]; - NSData *const apnsDeviceToken = ably.instanceStore.didRegisterForRemoteNotificationsWithDeviceToken_deviceToken; - NSError *const error = ably.instanceStore.didFailToRegisterForRemoteNotificationsWithError_error; - if (apnsDeviceToken != nil) { - [ARTPush didRegisterForRemoteNotificationsWithDeviceToken:apnsDeviceToken rest:rest]; - } else if (error != nil) { - [ARTPush didFailToRegisterForRemoteNotificationsWithError:error rest:rest]; - } - result(handle); }; @@ -262,19 +254,6 @@ -(void)reset; } ARTRealtime *const realtime = [[ARTRealtime alloc] initWithOptions:options.clientOptions]; [instanceStore setRealtime:realtime with:handle]; - - // Giving Ably client the deviceToken registered at device launch (didRegisterForRemoteNotificationsWithDeviceToken). - // This is not an ideal solution. We save the deviceToken given in didRegisterForRemoteNotificationsWithDeviceToken and the - // error in didFailToRegisterForRemoteNotificationsWithError and pass it to Ably in the first client that is first created. - // Ideally, the Ably client doesn't need to be created, and we can pass the deviceToken to Ably like in Ably Java. - // This is similarly repeated for in _createRest - NSData *const apnsDeviceToken = ably.instanceStore.didRegisterForRemoteNotificationsWithDeviceToken_deviceToken; - NSError *const error = ably.instanceStore.didFailToRegisterForRemoteNotificationsWithError_error; - if (apnsDeviceToken != nil) { - [ARTPush didRegisterForRemoteNotificationsWithDeviceToken:apnsDeviceToken realtime:realtime]; - } else if (error != nil) { - [ARTPush didFailToRegisterForRemoteNotificationsWithError:error realtime:realtime]; - } result(handle); }; @@ -801,7 +780,6 @@ -(void)reset { } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - [[UIApplication sharedApplication] registerForRemoteNotifications]; // Check if application was launched from a notification tap. // https://stackoverflow.com/a/21611009/7365866 @@ -814,16 +792,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( } #pragma mark - Push Notifications Registration - UIApplicationDelegate -/// Save the deviceToken provided so we can pass it to the first Ably client which gets created, in createRealtime or createRest. + -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { - // Set deviceToken on all existing Ably clients, and a property which used for all future Ably clients. [_instanceStore didRegisterForRemoteNotificationsWithDeviceToken: deviceToken]; } -/// Save the error if it occurred during APNs device registration provided so we can pass it to the first Ably client which gets created, in createRealtime or createRest. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { - // This error will be used when the first Ably client is made. - _instanceStore.didFailToRegisterForRemoteNotificationsWithError_error = error; + [_instanceStore didFailToRegisterForRemoteNotificationsWithError:error]; } - (BOOL)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { diff --git a/ios/Classes/AblyInstanceStore.h b/ios/Classes/AblyInstanceStore.h index 8af0edbc1..8cb838d95 100644 --- a/ios/Classes/AblyInstanceStore.h +++ b/ios/Classes/AblyInstanceStore.h @@ -25,11 +25,9 @@ NS_ASSUME_NONNULL_BEGIN -(ARTPaginatedResult *) getPaginatedResult:(NSNumber *const) handle; --(void) didRegisterForRemoteNotificationsWithDeviceToken:(NSData *const) deviceToken; +-(void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *const)deviceToken; -@property(nonatomic, nullable) NSData * didRegisterForRemoteNotificationsWithDeviceToken_deviceToken; - -@property(nonatomic, nullable) NSError * didFailToRegisterForRemoteNotificationsWithError_error; +-(void)didFailToRegisterForRemoteNotificationsWithError:(NSError *const)error; -(void)reset; diff --git a/ios/Classes/AblyInstanceStore.m b/ios/Classes/AblyInstanceStore.m index 7ef35ab4d..99ced5c49 100644 --- a/ios/Classes/AblyInstanceStore.m +++ b/ios/Classes/AblyInstanceStore.m @@ -65,20 +65,21 @@ -(ARTPaginatedResult *) getPaginatedResult:(NSNumber *const) handle { return _paginatedResults[handle]; } -// Set device token on all existing clients --(void) didRegisterForRemoteNotificationsWithDeviceToken:(NSData *const) deviceToken { - _didRegisterForRemoteNotificationsWithDeviceToken_deviceToken = deviceToken; - - for (id restHandle in _restInstances) { - ARTRest *const rest = _restInstances[restHandle]; - [ARTPush didRegisterForRemoteNotificationsWithDeviceToken:deviceToken rest:rest]; - } - for (id realtimeHandle in _realtimeInstances) { - ARTRealtime *const realtime = _realtimeInstances[realtimeHandle]; +// Set device token for the first created realtime object, device of which is available for others, because it's static +-(void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *const)deviceToken { + ARTRealtime *const realtime = _realtimeInstances.allValues.firstObject; + if (realtime) { [ARTPush didRegisterForRemoteNotificationsWithDeviceToken:deviceToken realtime:realtime]; } } +-(void)didFailToRegisterForRemoteNotificationsWithError:(NSError *const)error { + ARTRealtime *const realtime = _realtimeInstances.allValues.firstObject; + if (realtime) { + [ARTPush didFailToRegisterForRemoteNotificationsWithError:error realtime:realtime]; + } +} + -(void)reset { for (ARTRealtime *const r in _realtimeInstances.allValues) { [r close];