diff --git a/src/ios/PushPlugin.h b/src/ios/PushPlugin.h index 276a00807..5b261eac0 100644 --- a/src/ios/PushPlugin.h +++ b/src/ios/PushPlugin.h @@ -29,7 +29,7 @@ @protocol GGLInstanceIDDelegate; @protocol GCMReceiverDelegate; -@interface PushPlugin : CDVPlugin +@interface PushPlugin : CDVPlugin { NSDictionary *notificationMessage; BOOL isInline; @@ -69,7 +69,7 @@ - (void)didDeleteMessagesOnServer; // GCM Features -@property(nonatomic, assign) BOOL usesGCM; +@property(nonatomic, assign) BOOL usesFCM; @property(nonatomic, strong) NSNumber* gcmSandbox; @property(nonatomic, strong) NSString *gcmSenderId; @property(nonatomic, strong) NSDictionary *gcmRegistrationOptions; diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index a176b9af5..3aa4da987 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -27,8 +27,7 @@ #define GMP_NO_MODULES true #import "PushPlugin.h" -#import "GoogleCloudMessaging.h" -#import "GGLInstanceIDHeaders.h" +#import "Firebase.h" @implementation PushPlugin : CDVPlugin @@ -42,7 +41,7 @@ @implementation PushPlugin : CDVPlugin @synthesize clearBadge; @synthesize handlerObj; -@synthesize usesGCM; +@synthesize usesFCM; @synthesize gcmSandbox; @synthesize gcmSenderId; @synthesize gcmRegistrationOptions; @@ -62,22 +61,8 @@ -(void)initGCMRegistrationHandler; if (topics != nil) { for (NSString *topic in topics) { NSLog(@"subscribe from topic: %@", topic); - id pubSub = [GCMPubSub sharedInstance]; - [pubSub subscribeWithToken: [weakSelf gcmRegistrationToken] - topic:[NSString stringWithFormat:@"/topics/%@", topic] - options:nil - handler:^void(NSError *error) { - if (error) { - if (error.code == 3001) { - NSLog(@"Already subscribed to %@", topic); - } else { - NSLog(@"Failed to subscribe to topic %@: %@", topic, error); - } - } - else { - NSLog(@"Successfully subscribe to topic %@", topic); - } - }]; + id pubSub = [FIRInstanceID instanceID]; + [pubSub subscribeToTopic:[NSString stringWithFormat:@"/topics/%@", topic]]; } } @@ -89,31 +74,29 @@ -(void)initGCMRegistrationHandler; }; } -// GCM refresh token +// FCM refresh token // Unclear how this is testable under normal circumstances - (void)onTokenRefresh { #if !TARGET_IPHONE_SIMULATOR // A rotation of the registration tokens is happening, so the app needs to request a new token. - NSLog(@"The GCM registration token needs to be changed."); - [[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:[self gcmSenderId] - scope:kGGLInstanceIDScopeGCM - options:[self gcmRegistrationOptions] - handler:[self gcmRegistrationHandler]]; + NSLog(@"The FCM registration token needs to be changed."); + [[FIRInstanceID instanceID] token]; + [self gcmRegistrationHandler]; #endif } -- (void)willSendDataMessageWithID:(NSString *)messageID error:(NSError *)error { - NSLog(@"willSendDataMessageWithID"); - if (error) { - // Failed to send the message. - } else { - // Will send message, you can save the messageID to track the message - } +// contains error info +- (void)sendDataMessageFailure:(NSNotification *)notification { + NSString *messageID = (NSString *)notification.object; + NSDictionary *userInfo = notification.userInfo; + NSLog(@"sendDataMessageFailure"); + // Did fail send message } - -- (void)didSendDataMessageWithID:(NSString *)messageID { - NSLog(@"willSendDataMessageWithID"); - // Did successfully send message identified by messageID +- (void)sendDataMessageSuccess:(NSNotification *)notification { + NSString *messageID = (NSString *)notification.object; + NSDictionary *userInfo = notification.userInfo; + NSLog(@"sendDataMessageSuccess"); + // Did successfully send message } - (void)didDeleteMessagesOnServer { @@ -128,20 +111,10 @@ - (void)unregister:(CDVInvokedUrlCommand*)command; NSArray* topics = [command argumentAtIndex:0]; if (topics != nil) { - id pubSub = [GCMPubSub sharedInstance]; + id pubSub = [FIRInstanceID instanceID]; for (NSString *topic in topics) { NSLog(@"unsubscribe from topic: %@", topic); - [pubSub unsubscribeWithToken: [self gcmRegistrationToken] - topic:[NSString stringWithFormat:@"/topics/%@", topic] - options:nil - handler:^void(NSError *error) { - if (error) { - NSLog(@"Failed to unsubscribe from topic %@: %@", topic, error); - } - else { - NSLog(@"Successfully unsubscribe from topic %@", topic); - } - }]; + [pubSub unsubscribeFromTopic:topic]; } } else { [[UIApplication sharedApplication] unregisterForRemoteNotifications]; @@ -207,6 +180,22 @@ - (void)unsubscribe:(CDVInvokedUrlCommand*)command; - (void)init:(CDVInvokedUrlCommand*)command; { + [[NSNotificationCenter defaultCenter] + addObserver:self selector:@selector(onTokenRefresh) + name:kFIRInstanceIDTokenRefreshNotification object:nil]; + + [[NSNotificationCenter defaultCenter] + addObserver:self selector:@selector(sendDataMessageFailure:) + name:FIRMessagingSendErrorNotification object:nil]; + + [[NSNotificationCenter defaultCenter] + addObserver:self selector:@selector(sendDataMessageSuccess:) + name:FIRMessagingSendSuccessNotification object:nil]; + + [[NSNotificationCenter defaultCenter] + addObserver:self selector:@selector(didDeleteMessagesOnServer) + name:FIRMessagingMessagesDeletedNotification object:nil]; + [self.commandDelegate runInBackground:^ { NSLog(@"Push Plugin register called"); @@ -350,19 +339,22 @@ - (void)init:(CDVInvokedUrlCommand*)command; // GCM options [self setGcmSenderId: [iosOptions objectForKey:@"senderID"]]; - NSLog(@"GCM Sender ID %@", gcmSenderId); + NSLog(@"FCM Sender ID %@", gcmSenderId); if([[self gcmSenderId] length] > 0) { - NSLog(@"Using GCM Notification"); - [self setUsesGCM: YES]; + NSLog(@"Using FCM Notification"); + [self setUsesFCM: YES]; + dispatch_async(dispatch_get_main_queue(), ^{ + [FIRApp configure]; + }); [self initGCMRegistrationHandler]; } else { NSLog(@"Using APNS Notification"); - [self setUsesGCM:NO]; + [self setUsesFCM:NO]; } id gcmSandBoxArg = [iosOptions objectForKey:@"gcmSandbox"]; [self setGcmSandbox:@NO]; - if ([self usesGCM] && + if ([self usesFCM] && (([gcmSandBoxArg isKindOfClass:[NSString class]] && [gcmSandBoxArg isEqualToString:@"true"]) || [gcmSandBoxArg boolValue])) { @@ -460,22 +452,9 @@ - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [results setValue:dev.model forKey:@"deviceModel"]; [results setValue:dev.systemVersion forKey:@"deviceSystemVersion"]; - if([self usesGCM]) { - GGLInstanceIDConfig *instanceIDConfig = [GGLInstanceIDConfig defaultConfig]; - instanceIDConfig.delegate = self; - [[GGLInstanceID sharedInstance] startWithConfig:instanceIDConfig]; - - [self setGcmRegistrationOptions: @{kGGLInstanceIDRegisterAPNSOption:deviceToken, - kGGLInstanceIDAPNSServerTypeSandboxOption:[self gcmSandbox]}]; - - [[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:[self gcmSenderId] - scope:kGGLInstanceIDScopeGCM - options:[self gcmRegistrationOptions] - handler:[self gcmRegistrationHandler]]; + if([self usesFCM]) { - GCMConfig *gcmConfig = [GCMConfig defaultConfig]; - gcmConfig.receiverDelegate = self; - [[GCMService sharedInstance] startWithConfig:gcmConfig]; + NSLog(@"token FCM %@",[[FIRInstanceID instanceID] token]); } else { [self registerWithToken: token];