Commit 5148d0ac authored by JTB's avatar JTB
Browse files

Initial pass at support ios gcm

parent fbc26913
Loading
Loading
Loading
Loading
+52 −0
Changes for plugin.xml: 52 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -100,12 +100,64 @@
            </array>
        </config-file>
        
                <!-- Plugin Code -->
		<source-file src="src/ios/AppDelegate+notification.m" />
		<source-file src="src/ios/PushPlugin.m" />

		<header-file src="src/ios/AppDelegate+notification.h" />
		<header-file src="src/ios/PushPlugin.h" />

                <!--  GCM Provided Frameworks Used -->
                <framework src="AddressBook.framework" />
                <!-- Note these 2 appear in red in app code project but work-->
                <framework src="libsqlite3.tbd" />
                <framework src="libz.tbd" />

                <!--  GCM Code from Cocoapods  -->
		<header-file target-dir="GCM/Headers" src="src/ios/GGLInstanceID/Headers/Public/GGLInstanceID.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/GGLInstanceID/Headers/Public/GGLInstanceIDConfig.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/GGLInstanceID/Headers/Public/GGLInstanceIDDelegate.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/GGLInstanceID/Headers/Public/GGLInstanceIDHeaders.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCloudMessaging/Public/CloudMessaging.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCloudMessaging/Public/GGLContext+CloudMessaging.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCore/Public/Core.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCore/Public/GGLConfiguration.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCore/Public/GGLContext.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/Google/Headers/GGLCore/Public/GGLErrorCode.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/GoogleCloudMessaging/Headers/Public/GCMConfig.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/GoogleCloudMessaging/Headers/Public/GCMPubSub.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/GoogleCloudMessaging/Headers/Public/GCMReceiverDelegate.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/GoogleCloudMessaging/Headers/Public/GCMService.h" />
		<header-file target-dir="GCM/Headers" src="src/ios/GoogleCloudMessaging/Headers/Public/GoogleCloudMessaging.h" />

                <source-file target-dir="GCM/Libraries" src="src/ios/Google/Libraries/libGGLCloudMessaging.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/Google/Libraries/libGGLCore.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GGLInstanceID/Libraries/libGGLInstanceIDLib.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleIPhoneUtilities/Libraries/libGIP_Reachability.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleSymbolUtilities/Libraries/libGSDK_Overload.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleNetworkingUtilities/Libraries/libGTMSessionFetcher_core.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleNetworkingUtilities/Libraries/libGTMSessionFetcher_full.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTMStackTrace.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_AddressBook.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_DebugUtils.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_GTMURLBuilder.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_KVO.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_NSData+zlib.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_NSDictionary+URLArguments.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_NSScannerJSON.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_NSStringHTML.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_NSStringXML.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_Regex.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_RoundedRectPath.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_StringEncoding.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_SystemVersion.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_UIFont+LineHeight.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_core.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleUtilities/Libraries/libGTM_iPhone.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleCloudMessaging/Libraries/libGcmLib.a" framework="true" />
                <source-file target-dir="GCM/Libraries" src="src/ios/GoogleInterchangeUtilities/Libraries/libProtocolBuffers.a" framework="true" />
                <!--  End GCM Related Files -->

	</platform>

  	<!-- windows -->
+9 −1
Changes for src/ios/PushPlugin.h: 9 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -27,7 +27,8 @@
#import <Cordova/CDV.h>
#import <Cordova/CDVPlugin.h>

@interface PushPlugin : CDVPlugin
@protocol GGLInstanceIDDelegate;
@interface PushPlugin : CDVPlugin<GGLInstanceIDDelegate>
{
    NSDictionary *notificationMessage;
    BOOL    isInline;
@@ -59,4 +60,11 @@
- (void)setNotificationMessage:(NSDictionary *)notification;
- (void)notificationReceived;

//  GCM Features
@property(nonatomic, assign) BOOL usesGCM;
@property(nonatomic, assign) BOOL gcmSandbox;
@property(nonatomic, assign) NSString *gcmSenderId;
@property(nonatomic, strong) NSDictionary *gcmRegistrationOptions;
@property(nonatomic, strong) void (^gcmRegistrationHandler) (NSString *registrationToken, NSError *error);

@end
+88 −6
Changes for src/ios/PushPlugin.m: 88 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -23,7 +23,12 @@
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//  See GGLInstanceID.h
#define GMP_NO_MODULES true

#import "PushPlugin.h"
#import "CloudMessaging.h"


@implementation PushPlugin

@@ -36,6 +41,40 @@
@synthesize clearBadge;
@synthesize handlerObj;

@synthesize usesGCM;
@synthesize gcmSandbox;
@synthesize gcmSenderId;
@synthesize gcmRegistrationOptions;
@synthesize gcmRegistrationHandler;


-(void)initGCMRegistrationHandler;
{
    __weak __block PushPlugin *weakSelf = self;
    gcmRegistrationHandler = ^(NSString *registrationToken, NSError *error){
        if (registrationToken != nil) {
            NSLog(@"GCM Registration Token: %@", registrationToken);
            [weakSelf registerWithToken:registrationToken];
        } else {
            NSLog(@"Registration to GCM failed with error: %@", error.localizedDescription);
            [weakSelf failWithMessage:@"" withError:error];
        }
    };
}

//  GCM 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]];
#endif
}
     
- (void)unregister:(CDVInvokedUrlCommand*)command;
{
    self.callbackId = command.callbackId;
@@ -122,6 +161,29 @@
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif
    
    //  GCM options
    [self setGcmSenderId: [iosOptions objectForKey:@"senderId"]];
    if([[self gcmSenderId] length] > 0) {
        NSLog(@"Using GCM Notification");
        [self setUsesGCM: YES];
        GCMConfig *gcmConfig = [GCMConfig defaultConfig];
        [[GCMService sharedInstance] startWithConfig:gcmConfig];
        [self initGCMRegistrationHandler];
    } else {
        NSLog(@"Using APNS Notification");
        [self setUsesGCM:NO];
    }
    id gcmSandBoxArg = [iosOptions objectForKey:@"gcmSandbox"];

    [self setGcmSandbox:NO];
    if ([self usesGCM] &&
        (([gcmSandBoxArg isKindOfClass:[NSString class]] && [gcmSandBoxArg isEqualToString:@"true"]) ||
            [gcmSandBoxArg boolValue]))
    {
        NSLog(@"Using GCM Sandbox");
        [self setGcmSandbox:YES];
    }
    
    if (notificationMessage)			// if there is a pending startup notification
        [self notificationReceived];	// go ahead and process it

@@ -185,12 +247,22 @@
    [results setValue:dev.model forKey:@"deviceModel"];
    [results setValue:dev.systemVersion forKey:@"deviceSystemVersion"];

    // Send result to trigger 'registration' event but keep callback
    NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:1];
    [message setObject:token forKey:@"registrationId"];
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message];
    [pluginResult setKeepCallbackAsBool:YES];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
    if([self usesGCM]) {
        GGLInstanceIDConfig *instanceIDConfig = [GGLInstanceIDConfig defaultConfig];
        instanceIDConfig.delegate = self;
        [[GGLInstanceID sharedInstance] startWithConfig:instanceIDConfig];
        
        [self setGcmRegistrationOptions: @{kGGLInstanceIDRegisterAPNSOption:deviceToken,
                                     kGGLInstanceIDAPNSServerTypeSandboxOption:@YES}];
        
        [[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:[self gcmSenderId]
                                                            scope:kGGLInstanceIDScopeGCM
                                                          options:[self gcmRegistrationOptions]
                                                          handler:[self gcmRegistrationHandler]];

    } else {
        [self registerWithToken: token];
    }
#endif
}

@@ -300,6 +372,16 @@
    }
}

-(void)registerWithToken:(NSString*)token; {
    // Send result to trigger 'registration' event but keep callback
    NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:1];
    [message setObject:token forKey:@"registrationId"];
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message];
    [pluginResult setKeepCallbackAsBool:YES];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}


-(void)failWithMessage:(NSString *)message withError:(NSError *)error
{
    NSString        *errorMessage = (error) ? [NSString stringWithFormat:@"%@ - %@", message, [error localizedDescription]] : message;