Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Changed code to work with FCM
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored and macdonst committed Jan 12, 2017
1 parent a65be29 commit f7acf33
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/ios/PushPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

@protocol GGLInstanceIDDelegate;
@protocol GCMReceiverDelegate;
@interface PushPlugin : CDVPlugin<GGLInstanceIDDelegate, GCMReceiverDelegate>
@interface PushPlugin : CDVPlugin
{
NSDictionary *notificationMessage;
BOOL isInline;
Expand Down Expand Up @@ -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;
Expand Down
115 changes: 47 additions & 68 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
#define GMP_NO_MODULES true

#import "PushPlugin.h"
#import "GoogleCloudMessaging.h"
#import "GGLInstanceIDHeaders.h"
#import "Firebase.h"

@implementation PushPlugin : CDVPlugin

Expand All @@ -42,7 +41,7 @@ @implementation PushPlugin : CDVPlugin
@synthesize clearBadge;
@synthesize handlerObj;

@synthesize usesGCM;
@synthesize usesFCM;
@synthesize gcmSandbox;
@synthesize gcmSenderId;
@synthesize gcmRegistrationOptions;
Expand All @@ -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]];
}
}

Expand All @@ -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 {
Expand All @@ -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];
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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]))
{
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit f7acf33

Please sign in to comment.