Skip to content

Commit

Permalink
Merge commit '3a1eda75f5f6500e1d040b4908970d8127533587' into v1.5.3-i…
Browse files Browse the repository at this point in the history
…os-gcm-topics

* commit '3a1eda75f5f6500e1d040b4908970d8127533587':
  Issue phonegap#79: Subscribe to Topics for iOS
  Issue phonegap#492: GoogleService failed to initialize

Conflicts:
	docs/INSTALLATION.md
  • Loading branch information
denisbabineau committed Feb 22, 2016
2 parents f598096 + 3a1eda7 commit 86bf5fa
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 18 deletions.
11 changes: 6 additions & 5 deletions docs/INSTALLATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@
This requires phonegap/cordova CLI 5.0+ ( current stable v1.5.3 )

```
phonegap plugin add phonegap-plugin-push
phonegap plugin add phonegap-plugin-push --variable SENDER_ID="XXXXXXX"
```
or

```
cordova plugin add phonegap-plugin-push
cordova plugin add phonegap-plugin-push --variable SENDER_ID="XXXXXXX"
```

It is also possible to install via repo url directly ( unstable )

```
phonegap plugin add https://github.com/phonegap/phonegap-plugin-push
phonegap plugin add https://github.com/phonegap/phonegap-plugin-push --variable SENDER_ID="XXXXXXX"
```

or

```
cordova plugin add https://github.com/phonegap/phonegap-plugin-push
cordova plugin add https://github.com/phonegap/phonegap-plugin-push --variable SENDER_ID="XXXXXXX"
```

Where the `XXXXXXX` in `SENDER_ID="XXXXXXX"` maps to the project number in the Google Developer Console. If you are not creating an Android application you can put in anything for this value.

## Android details

### Compilation
Expand Down Expand Up @@ -131,7 +133,6 @@ Common plugins to suffer from this outdated dependency management are plugins re
### XCode

XCode version 7.0 or greater is required for building this plugin.

### Bitcode

If you are running into a problem where the linker is complaining about bit code. For instance:
Expand Down
19 changes: 19 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<engine name="cordova-android" version=">=4.0.0" />
</engines>

<preference name="SENDER_ID" />
<preference name="GOOGLE_APP_ID" />

<!-- android -->
<platform name="android">

Expand All @@ -34,6 +37,10 @@
</feature>
</config-file>

<config-file target="res/values/strings.xml" parent="/resources">
<string name="google_app_id">$SENDER_ID</string>
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down Expand Up @@ -105,6 +112,18 @@
</array>
</config-file>

<config-file target="*-Info.plist" parent="GCM_SENDER_ID">
<string>$SENDER_ID</string>
</config-file>

<config-file target="*-Info.plist" parent="IS_GCM_ENABLED">
<true/>
</config-file>

<config-file target="*-Info.plist" parent="GOOGLE_APP_ID">
<string>$GOOGLE_APP_ID</string>
</config-file>

<source-file src="src/ios/AppDelegate+notification.m" />
<source-file src="src/ios/PushPlugin.m" />

Expand Down
13 changes: 12 additions & 1 deletion src/ios/AppDelegate+notification.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#import <objc/runtime.h>

static char launchNotificationKey;

@implementation AppDelegate (notification)

- (id) getCommandInstance:(NSString*)className
Expand Down Expand Up @@ -62,6 +61,18 @@ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotif
[pushHandler didFailToRegisterForRemoteNotificationsWithError:error];
}

//- (void)application:(UIApplication *)application
//didReceiveRemoteNotification:(NSDictionary *)userInfo {
// NSLog(@"Notification received: %@", userInfo);
// This works only if the app started the GCM service
// [[GCMService sharedInstance] appDidReceiveMessage:userInfo];
// Handle the received message
// [START_EXCLUDE]
// [[NSNotificationCenter defaultCenter] postNotificationName:_messageKey
// object:nil
// userInfo:userInfo];
//}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"didReceiveNotification with fetchCompletionHandler");

Expand Down
21 changes: 14 additions & 7 deletions src/ios/PushPlugin.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*
Copyright 2009-2011 Urban Airship Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binaryform must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided withthe distribution.
THIS SOFTWARE IS PROVIDED BY THE URBAN AIRSHIP INC``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
Expand All @@ -28,17 +28,18 @@
#import <Cordova/CDVPlugin.h>

@protocol GGLInstanceIDDelegate;
@interface PushPlugin : CDVPlugin<GGLInstanceIDDelegate>
@protocol GCMReceiverDelegate;
@interface PushPlugin : CDVPlugin<GGLInstanceIDDelegate, GCMReceiverDelegate>
{
NSDictionary *notificationMessage;
BOOL isInline;
NSString *notificationCallbackId;
NSString *callback;
BOOL clearBadge;

NSDictionary *handlerObj;
void (^completionHandler)(UIBackgroundFetchResult);

BOOL ready;
}

Expand All @@ -60,11 +61,17 @@
- (void)setNotificationMessage:(NSDictionary *)notification;
- (void)notificationReceived;

- (void)willSendDataMessageWithID:(NSString *)messageID error:(NSError *)error;
- (void)didSendDataMessageWithID:(NSString *)messageID;
- (void)didDeleteMessagesOnServer;

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

@end
83 changes: 78 additions & 5 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
#import "PushPlugin.h"
#import "CloudMessaging.h"


@implementation PushPlugin
@implementation PushPlugin : CDVPlugin

@synthesize notificationMessage;
@synthesize isInline;
Expand All @@ -46,14 +45,40 @@ @implementation PushPlugin
@synthesize gcmSenderId;
@synthesize gcmRegistrationOptions;
@synthesize gcmRegistrationHandler;

@synthesize gcmRegistrationToken;
@synthesize gcmTopics;

-(void)initGCMRegistrationHandler;
{
__weak __block PushPlugin *weakSelf = self;
gcmRegistrationHandler = ^(NSString *registrationToken, NSError *error){
if (registrationToken != nil) {
NSLog(@"GCM Registration Token: %@", registrationToken);
[weakSelf setGcmRegistrationToken: registrationToken];

id topics = [weakSelf gcmTopics];
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);
}
}];
}
}

[weakSelf registerWithToken:registrationToken];
} else {
NSLog(@"Registration to GCM failed with error: %@", error.localizedDescription);
Expand All @@ -75,12 +100,53 @@ - (void)onTokenRefresh {
#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
}
}

- (void)didSendDataMessageWithID:(NSString *)messageID {
NSLog(@"willSendDataMessageWithID");
// Did successfully send message identified by messageID
}

- (void)didDeleteMessagesOnServer {
NSLog(@"didDeleteMessagesOnServer");
// Some messages sent to this device were deleted on the GCM server before reception, likely
// because the TTL expired. The client should notify the app server of this, so that the app
// server can resend those messages.
}

- (void)unregister:(CDVInvokedUrlCommand*)command;
{
self.callbackId = command.callbackId;

[[UIApplication sharedApplication] unregisterForRemoteNotifications];
[self successWithMessage:@"unregistered"];
NSArray* topics = [command argumentAtIndex:0];

if (topics != nil) {
id pubSub = [GCMPubSub sharedInstance];
for (NSString *topic in topics) {
NSLog(@"unsubscribe from topic: %@", topic);
[pubSub unsubscribeWithToken: [self gcmRegistrationToken]
topic:topic
options:nil
handler:^void(NSError *error) {
if (error) {
NSLog(@"Failed to unsubscribe from topic %@: %@", topic, error);
}
else {
NSLog(@"Successfully unsubscribe from topic %@", topic);
}
}];
}
} else {
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
[self successWithMessage:@"unregistered"];
}
}

- (void)init:(CDVInvokedUrlCommand*)command;
Expand All @@ -93,6 +159,9 @@ - (void)init:(CDVInvokedUrlCommand*)command;
NSMutableDictionary* options = [command.arguments objectAtIndex:0];
NSMutableDictionary* iosOptions = [options objectForKey:@"ios"];

NSArray* topics = [iosOptions objectForKey:@"topics"];
[self setGcmTopics:topics];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
UIUserNotificationType UserNotificationTypes = UIUserNotificationTypeNone;
#endif
Expand Down Expand Up @@ -341,6 +410,10 @@ - (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
options:[self gcmRegistrationOptions]
handler:[self gcmRegistrationHandler]];

GCMConfig *gcmConfig = [GCMConfig defaultConfig];
gcmConfig.receiverDelegate = self;
[[GCMService sharedInstance] startWithConfig:gcmConfig];

} else {
[self registerWithToken: token];
}
Expand Down

0 comments on commit 86bf5fa

Please sign in to comment.