Skip to content

Commit

Permalink
feat(ios)!: PushPluginFCM - move more FCM logic over
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu committed Oct 16, 2024
1 parent 9631ba2 commit fb41af5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 44 deletions.
40 changes: 7 additions & 33 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
@interface PushPlugin ()

@property (nonatomic, strong) PushPluginFCM *pushPluginFCM;
@property (nonatomic, assign) BOOL isFCMRefreshTokenObserverAttached;

@end

Expand All @@ -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];

Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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]];
}
Expand Down Expand Up @@ -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];
Expand Down
9 changes: 5 additions & 4 deletions src/ios/PushPluginFCM.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h>

@import Firebase;

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 <CDVCommandDelegate>)commandDelegate;
- (void)configureTokens:(NSData *)token;

- (void)subscribeToTopic:(NSString *)topic;
- (void)subscribeToTopics:(NSArray *)topics;

- (void)unsubscribeFromTopic:(NSString *)topic;
- (void)unsubscribeFromTopics:(NSArray *)topics;
Expand Down
59 changes: 52 additions & 7 deletions src/ios/PushPluginFCM.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#import "PushPluginFCM.h"
#import "PushPluginSettings.h"

@interface PushPluginFCM ()

@property (nonatomic, assign) BOOL isFCMRefreshTokenObserverAttached;
@property (nonatomic, weak) id <CDVCommandDelegate> commandDelegate;

@end

@implementation PushPluginFCM

- (instancetype)initWithGoogleServicePlist {
Expand Down Expand Up @@ -28,34 +35,72 @@ - (instancetype)initWithGoogleServicePlist {
return self;
}

- (void)configure {
- (void)configure:(id <CDVCommandDelegate>)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.");
Expand Down

0 comments on commit fb41af5

Please sign in to comment.