Skip to content

Commit

Permalink
Removed all push handling from instance store. Made `didRegisterForRe…
Browse files Browse the repository at this point in the history
…moteNotificationsWithDeviceToken` on a proper instance of the realtime/rest that called push activation.
  • Loading branch information
maratal committed Aug 10, 2024
1 parent 25ef834 commit 18d1207
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
24 changes: 21 additions & 3 deletions ios/Classes/AblyFlutter.m
Original file line number Diff line number Diff line change
Expand Up @@ -793,12 +793,30 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

#pragma mark - Push Notifications Registration - UIApplicationDelegate

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[_instanceStore didRegisterForRemoteNotificationsWithDeviceToken: deviceToken];
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
ARTRealtime *const realtime = [_instanceStore realtimeFrom:PushHandlers.pushActivatorHandle];
if (realtime) {
[ARTPush didRegisterForRemoteNotificationsWithDeviceToken:deviceToken realtime:realtime];
}
else {
ARTRest *const rest = [_instanceStore restFrom:PushHandlers.pushActivatorHandle];
if (rest) {
[ARTPush didRegisterForRemoteNotificationsWithDeviceToken:deviceToken rest:rest];
}
}
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[_instanceStore didFailToRegisterForRemoteNotificationsWithError:error];
ARTRealtime *const realtime = [_instanceStore realtimeFrom:PushHandlers.pushActivatorHandle];
if (realtime) {
[ARTPush didFailToRegisterForRemoteNotificationsWithError:error realtime:realtime];
}
else {
ARTRest *const rest = [_instanceStore restFrom:PushHandlers.pushActivatorHandle];
if (rest) {
[ARTPush didFailToRegisterForRemoteNotificationsWithError:error rest:rest];
}
}
}

- (BOOL)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
Expand Down
4 changes: 0 additions & 4 deletions ios/Classes/AblyInstanceStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ NS_ASSUME_NONNULL_BEGIN

-(ARTPaginatedResult *) getPaginatedResult:(NSNumber *const) handle;

-(void) didRegisterForRemoteNotificationsWithDeviceToken:(NSData *const) deviceToken;

-(void)didFailToRegisterForRemoteNotificationsWithError:(NSError *const)error;

-(void)reset;

@end
Expand Down
15 changes: 0 additions & 15 deletions ios/Classes/AblyInstanceStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,6 @@ -(ARTPaginatedResult *) getPaginatedResult:(NSNumber *const) handle {
return _paginatedResults[handle];
}

// 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
6 changes: 6 additions & 0 deletions ios/Classes/handlers/PushHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import Foundation
import UserNotifications

public class PushHandlers: NSObject {

@objc
public static var pushActivatorHandle: NSNumber?

@objc
public static var pushNotificationTapLaunchedAppFromTerminatedData: Dictionary<AnyHashable, Any>? = nil;

@objc
public static let activate: FlutterHandler = { ably, call, result in
if let push = getPush(instanceStore: ably.instanceStore, call: call, result: result) {
pushActivatorHandle = (call.arguments as! AblyFlutterMessage).handle
PushActivationEventHandlers.getInstance(methodChannel: ably.channel).flutterResultForActivate = result
push.activate()
} else {
Expand All @@ -18,6 +23,7 @@ public class PushHandlers: NSObject {
@objc
public static let deactivate: FlutterHandler = { ably, call, result in
if let push = getPush(instanceStore: ably.instanceStore, call: call, result: result) {
pushActivatorHandle = nil
PushActivationEventHandlers.getInstance(methodChannel: ably.channel).flutterResultForDeactivate = result
push.deactivate()
} else {
Expand Down

0 comments on commit 18d1207

Please sign in to comment.