diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 728c8a6f9..7428f64bd 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -31,7 +31,6 @@ @interface PushPlugin () @property (nonatomic, strong) PushPluginFCM *pushPluginFCM; -@property (nonatomic, assign) BOOL isFCMRefreshTokenObserverAttached; @end @@ -49,33 +48,10 @@ - (void)pluginInitialize { self.pushPluginFCM = [[PushPluginFCM alloc] initWithGoogleServicePlist]; if([self.pushPluginFCM isFCMEnabled]) { - [self.pushPluginFCM configure]; + [self.pushPluginFCM configure:self.commandDelegate]; } } -- (void)setFCMTokenWithCompletion { - __weak __typeof(self) weakSelf = self; - [self.pushPluginFCM setTokenWithCompletion:^(NSString *token) { - [weakSelf registerWithToken:token]; - - if (!weakSelf.isFCMRefreshTokenObserverAttached) { - NSLog(@"[PushPlugin] Attaching FCM Token Refresh Observer"); - - [[NSNotificationCenter defaultCenter] addObserver:weakSelf - selector:@selector(setRefreshedFCMToken) - name:[PushPluginFCM pushPluginFCMMessagingRegistrationTokenRefreshedNotification] - object:nil]; - - weakSelf.isFCMRefreshTokenObserverAttached = YES; - } - }]; -} - - - (void)setRefreshedFCMToken { - NSLog(@"[PushPlugin] FIR has triggered a token refresh."); - [self setFCMTokenWithCompletion]; - } - - (void)unregister:(CDVInvokedUrlCommand *)command { NSArray* topics = [command argumentAtIndex:0]; @@ -128,6 +104,10 @@ - (void)init:(CDVInvokedUrlCommand *)command { [[PushPluginSettings sharedInstance] updateSettingsWithOptions:[options objectForKey:@"ios"]]; PushPluginSettings *settings = [PushPluginSettings sharedInstance]; + if ([self.pushPluginFCM isFCMEnabled]) { + self.pushPluginFCM.callbackId = command.callbackId; + } + self.callbackId = command.callbackId; if ([settings voipEnabled]) { @@ -189,8 +169,7 @@ - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"[PushPlugin] register success: %@", deviceToken); if ([self.pushPluginFCM isFCMEnabled]) { - [self.pushPluginFCM setAPNSToken:deviceToken]; - [self setFCMTokenWithCompletion]; + [self.pushPluginFCM configureTokens:deviceToken]; } else { [self registerWithToken:[self convertTokenToString:deviceToken]]; } @@ -372,14 +351,9 @@ - (void)successWithMessage:(NSString *)myCallbackId withMsg:(NSString *)message } - (void)registerWithToken:(NSString *)token { - NSString* registrationType = @"APNS"; - if ([self.pushPluginFCM isFCMEnabled]) { - registrationType = @"FCM"; - } - NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:2]; [message setObject:token forKey:@"registrationId"]; - [message setObject:registrationType forKey:@"registrationType"]; + [message setObject:@"APNS" forKey:@"registrationType"]; // Send result to trigger 'registration' event but keep callback CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message]; diff --git a/src/ios/PushPluginFCM.h b/src/ios/PushPluginFCM.h index 7fc7695e8..7bd7df1cf 100644 --- a/src/ios/PushPluginFCM.h +++ b/src/ios/PushPluginFCM.h @@ -1,4 +1,6 @@ #import +#import + @import Firebase; NS_ASSUME_NONNULL_BEGIN @@ -6,15 +8,14 @@ NS_ASSUME_NONNULL_BEGIN @interface PushPluginFCM : NSObject @property (nonatomic, assign) BOOL isFCMEnabled; +@property (nonatomic, strong) NSString *callbackId; - (instancetype)initWithGoogleServicePlist; -- (void)configure; -- (void)setAPNSToken:(NSData *)token; -- (void)setTokenWithCompletion:(void (^)(NSString *token))completion; +- (void)configure:(id )commandDelegate; +- (void)configureTokens:(NSData *)token; - (void)subscribeToTopic:(NSString *)topic; -- (void)subscribeToTopics:(NSArray *)topics; - (void)unsubscribeFromTopic:(NSString *)topic; - (void)unsubscribeFromTopics:(NSArray *)topics; diff --git a/src/ios/PushPluginFCM.m b/src/ios/PushPluginFCM.m index 779ab2c65..3a8bbe1d7 100644 --- a/src/ios/PushPluginFCM.m +++ b/src/ios/PushPluginFCM.m @@ -1,6 +1,13 @@ #import "PushPluginFCM.h" #import "PushPluginSettings.h" +@interface PushPluginFCM () + +@property (nonatomic, assign) BOOL isFCMRefreshTokenObserverAttached; +@property (nonatomic, weak) id commandDelegate; + +@end + @implementation PushPluginFCM - (instancetype)initWithGoogleServicePlist { @@ -28,34 +35,72 @@ - (instancetype)initWithGoogleServicePlist { return self; } -- (void)configure { +- (void)configure:(id )commandDelegate { NSLog(@"[PushPlugin] Configuring Firebase App for FCM"); [FIRApp configure]; + + self.commandDelegate = commandDelegate; } -- (void)setAPNSToken:(NSData *)token { +- (void)configureTokens:(NSData *)token { NSLog(@"[PushPlugin] Setting APNS Token for Firebase App"); [[FIRMessaging messaging] setAPNSToken:token]; + + [self setFCMTokenWithCompletion]; +} + +- (void)setRefreshedFCMToken { + NSLog(@"[PushPlugin] FIR has triggered a token refresh."); + [self setFCMTokenWithCompletion]; } -- (void)setTokenWithCompletion:(void (^)(NSString *token))completion { -#if TARGET_IPHONE_SIMULATOR +- (void)setFCMTokenWithCompletion { + #if TARGET_IPHONE_SIMULATOR NSLog(@"[PushPlugin] Detected simulator. Will register an FCM token but pushing to simulator is not possible."); -#endif + #endif + __weak __typeof(self) weakSelf = self; [[FIRMessaging messaging] tokenWithCompletion:^(NSString *token, NSError *error) { if (error != nil) { NSLog(@"[PushPlugin] Error getting FCM registration token: %@", error); } else { NSLog(@"[PushPlugin] FCM registration token: %@", token); [self subscribeToTopics:[PushPluginSettings sharedInstance].fcmTopics]; - if (completion) { - completion(token); + + NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:2]; + [message setObject:token forKey:@"registrationId"]; + [message setObject:@"FCM" forKey:@"registrationType"]; + + // Send result to trigger 'registration' event but keep callback + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message]; + [pluginResult setKeepCallbackAsBool:YES]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:weakSelf.callbackId]; + + if (!weakSelf.isFCMRefreshTokenObserverAttached) { + NSLog(@"[PushPlugin] Attaching FCM Token Refresh Observer"); + + [[NSNotificationCenter defaultCenter] addObserver:weakSelf + selector:@selector(setRefreshedFCMToken) + name:[PushPluginFCM pushPluginFCMMessagingRegistrationTokenRefreshedNotification] + object:nil]; + + weakSelf.isFCMRefreshTokenObserverAttached = YES; } } }]; } +- (void)registerWithToken:(NSString *)token { + NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:2]; + [message setObject:token forKey:@"registrationId"]; + [message setObject:@"FCM" forKey:@"registrationType"]; + + // Send result to trigger 'registration' event but keep callback + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message]; + [pluginResult setKeepCallbackAsBool:YES]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId]; +} + - (void)subscribeToTopics:(NSArray *)topics { if(!topics) { NSLog(@"[PushPlugin] There are no topics to subscribe to.");