Skip to content

Commit

Permalink
Issue phonegap#79: Subscribe to Topics for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst authored and hagish committed Feb 24, 2016
1 parent ffd103a commit a5168e9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/ios/PushPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
#import <Cordova/CDVPlugin.h>

@protocol GGLInstanceIDDelegate;
@interface PushPlugin : CDVPlugin<GGLInstanceIDDelegate>
@protocol GCMReceiverDelegate;
@interface PushPlugin : CDVPlugin<GGLInstanceIDDelegate, GCMReceiverDelegate>
{
NSDictionary *notificationMessage;
BOOL isInline;
Expand Down Expand Up @@ -67,5 +68,7 @@
@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
94 changes: 90 additions & 4 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#import "PushPlugin.h"
#import "CloudMessaging.h"


@implementation PushPlugin

@synthesize notificationMessage;
Expand All @@ -47,14 +46,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 @@ -76,12 +101,52 @@ - (void)onTokenRefresh {
#endif
}

// [START upstream_callbacks]
- (void)willSendDataMessageWithID:(NSString *)messageID error:(NSError *)error {
if (error) {
// Failed to send the message.
} else {
// Will send message, you can save the messageID to track the message
}
}

- (void)didSendDataMessageWithID:(NSString *)messageID {
// Did successfully send message identified by messageID
}
// [END upstream_callbacks]

- (void)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 @@ -94,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 @@ -346,6 +414,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 Expand Up @@ -534,4 +606,18 @@ -(void)stopBackgroundTask:(NSTimer*)timer
}
}

// [START ack_message_reception]
- (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];
// [END_EXCLUDE]
}

@end

0 comments on commit a5168e9

Please sign in to comment.