Skip to content

Commit

Permalink
Removed unnecessary push token handling. Token is requested by ably-c…
Browse files Browse the repository at this point in the history
…ocoa 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.
  • Loading branch information
maratal committed Jul 28, 2024
1 parent ca85d49 commit cbeaea0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 41 deletions.
29 changes: 2 additions & 27 deletions ios/Classes/AblyFlutter.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions ios/Classes/AblyInstanceStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
21 changes: 11 additions & 10 deletions ios/Classes/AblyInstanceStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit cbeaea0

Please sign in to comment.