Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for subscribe to topic , un-subscribe to topic #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/ios/AppDelegate+Notification.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
//
//

#ifndef TTTMobile_AppDelegate_Notification_h
#define TTTMobile_AppDelegate_Notification_h

#import "AppDelegate.h"

@interface AppDelegate (notification)

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
- (void) application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken;
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken;

@end

#endif
8 changes: 5 additions & 3 deletions src/ios/AppDelegate+Notification.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
//

#import "AppDelegate+notification.h"
#import "GCMPushPlugin.h"
#import <Google/CloudMessaging.h>

@implementation AppDelegate (notification)

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[self delegateNotification:userInfo];

completionHandler(UIBackgroundFetchResultNewData);
}

Expand All @@ -23,12 +25,12 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
- (void) delegateNotification:(NSDictionary *)userInfo {
[[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:@"pushNotification"];
[[NSUserDefaults standardUserDefaults] synchronize];

[[NSNotificationCenter defaultCenter] postNotificationName:@"GCMPushPluginRemoteNotification" object:userInfo];
}

- (void) application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
// We need to implement this here because apache cordova shortens the token whereas for GCM registration we need the full token.
// We need to implement this here because apache cordova shortens the token whereas for GCM registration we need the full token.
[[NSNotificationCenter defaultCenter] postNotificationName:CDVRemoteNotification object:deviceToken];
}

Expand Down
4 changes: 4 additions & 0 deletions src/ios/GCMPushPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
@property(nonatomic, strong) NSDictionary *pushNotification;
@property(nonatomic, strong) NSDictionary *registrationOptions;
@property(nonatomic, strong) void (^registrationHandler) (NSString *registrationToken, NSError *error);
@property(nonatomic, weak) NSString* registrationToken;

- (void)register:(CDVInvokedUrlCommand*)command;
- (void)unregister:(CDVInvokedUrlCommand*)command;
- (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand*)command;

-(void)subscribeToTopic:(CDVInvokedUrlCommand *)command;
-(void)unsubscribeToTopic:(CDVInvokedUrlCommand *)command;

@end
131 changes: 109 additions & 22 deletions src/ios/GCMPushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ @implementation GCMPushPlugin

- (CDVPlugin *)initWithWebView:(UIWebView *)theWebView {
NSLog(@"initializing GCMPushPlugin");

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRegisterForRemoteNotifications:)
name:CDVRemoteNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didFailToRegisterForRemoteNotifications:)
name:CDVRemoteNotificationError
Expand All @@ -27,6 +27,11 @@ - (CDVPlugin *)initWithWebView:(UIWebView *)theWebView {
name:UIApplicationDidBecomeActiveNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationinActive:)
name:UIApplicationWillTerminateNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pageDidLoad:)
name:CDVPageDidLoadNotification
Expand All @@ -42,92 +47,174 @@ - (void) applicationActive:(NSNotification*)notification {

- (void)pageDidLoad:(NSNotification*)notification {
self.pushNotification = [[NSUserDefaults standardUserDefaults] objectForKey:@"pushNotification"];

if (self.pushNotification) {
//Debug callback
// [[NSUserDefaults standardUserDefaults] setObject:@"alert" forKey:@"jsCallback"];
// [[NSUserDefaults standardUserDefaults] synchronize];

[[NSNotificationCenter defaultCenter] postNotificationName:@"GCMPushPluginRemoteNotification" object:self.pushNotification];
}
}

- (void) didRegisterForRemoteNotifications:(NSNotification*)notification {
NSData* token = [notification object];
NSLog(@"Token: %@", token);

if (self.usesGCM) {
NSError* configureError;
[[GGLContext sharedInstance] configureWithError:&configureError];
NSAssert(!configureError, @"Error configuring Google services: %@", configureError);

[[GCMService sharedInstance] startWithConfig:[GCMConfig defaultConfig]];

NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
__weak __block GCMPushPlugin *weakSelf = self;
_registrationHandler = ^(NSString *registrationToken, NSError *error){
if (registrationToken != nil) {
NSLog(@"Registration Token: %@", registrationToken);
NSDictionary *tokenResponse = @{@"gcm": registrationToken};


[userdefaults setObject:registrationToken forKey:@"registrationToken"];
[userdefaults synchronize];

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:tokenResponse];
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:weakSelf.registerCallbackId];
} else {
NSLog(@"Registration to GCM failed with error: %@", error.localizedDescription);

NSString *errorMessage = [NSString stringWithFormat:@"Error while registering for remote notifications: %@", error.localizedDescription];

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:weakSelf.registerCallbackId];
}
};

[[GGLInstanceID sharedInstance] startWithConfig:[GGLInstanceIDConfig defaultConfig]];

self.registrationOptions = @{kGGLInstanceIDRegisterAPNSOption:token,
kGGLInstanceIDAPNSServerTypeSandboxOption:@(self.gcmSandbox)};

[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:[[[GGLContext sharedInstance] configuration] gcmSenderID]
scope:kGGLInstanceIDScopeGCM
options:self.registrationOptions
handler:_registrationHandler];
} else {
NSLog(@"Registering with native iOS hooks");
NSDictionary *tokenResponse = @{@"ios": token};

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:tokenResponse];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.registerCallbackId];
}
}

- (void) didFailToRegisterForRemoteNotifications:(NSNotification*)notification {
NSError *error = [notification object];

NSString *errorMessage = [NSString stringWithFormat:@"Error while registering for remote notifications: %@", error.localizedDescription];

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.registerCallbackId];
}

- (void) didReceiveRemoteNotification:(NSNotification*)notification {
NSDictionary *pushNotification = [notification object];

self.jsCallback = [[NSUserDefaults standardUserDefaults] stringForKey:@"jsCallback"];
if (pushNotification && self.jsCallback) {
// NSLog(@"Received notification: %@", pushNotification);

NSMutableString *jsonStr = [NSMutableString stringWithString:@"{"];
[self parseDictionary:pushNotification intoJSON:jsonStr];
[jsonStr appendString:@"}"];

// NSLog(@"Msg: %@", jsonStr);
if (self.usesGCM) [[GCMService sharedInstance] appDidReceiveMessage:pushNotification];

// if (self.usesGCM) [[GCMService sharedInstance] appDidReceiveMessage:pushNotification];

NSString *js = [NSString stringWithFormat:@"%@(%@);", self.jsCallback, jsonStr];
[self.commandDelegate evalJs:js];
}
}

-(void)subscribeToTopic:(CDVInvokedUrlCommand *)command {

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

if (command.arguments.count > 0) {

[[GCMPubSub sharedInstance] subscribeWithToken:[userDefaults objectForKey:@"registrationToken"]
topic:[command.arguments firstObject]
options:nil
handler:^(NSError *error) {
if (error) {
// Treat the "already subscribed" error more gently
if (error.code == 3001) {
NSLog(@"Already subscribed to %@",
[command.arguments firstObject]);
} else {
NSLog(@"Subscription failed: %@",
error.localizedDescription);
}
NSString *responseString =@"subscriptionFailed";

CDVPluginResult *pluginResult =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:responseString];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} else {
// self.subscribedToTopic = true;
NSLog(@"Subscribed to %@", [command.arguments firstObject]);
NSString *responseString =@"topicSubscribed";

CDVPluginResult *pluginResult =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:responseString];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}
}

-(void)unsubscribeToTopic:(CDVInvokedUrlCommand *)command
{
NSUserDefaults *defaults= [NSUserDefaults standardUserDefaults];

if (command.arguments.count > 0)
{
[[GCMPubSub sharedInstance] unsubscribeWithToken:[defaults objectForKey:@"registrationToken"]
topic:[command.arguments firstObject]
options:nil
handler:^(NSError *error) {
if (error) {
// Treat the "already subscribed" error more gently
if (error.code == 3001) {
NSLog(@"Already unsubscribed to %@",
command.arguments.firstObject);
} else {
NSLog(@"UnSubscription failed: %@",
error.localizedDescription);
}
NSString *responseString =@"subscriptionFailed";

CDVPluginResult *pluginResult =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:responseString];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} else {
// self.subscribedToTopic = true;
NSLog(@"UnSubscribed to %@", [command.arguments firstObject]);

NSString *responseString =@"topicUnsubscribed";

CDVPluginResult *pluginResult =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:responseString];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}
}

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

Expand Down
17 changes: 17 additions & 0 deletions www/gpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,22 @@ var GcmPushPlugin = {
[options]
);
},
subscribeToTopic : function(successCB,errorCB, topic){
cordova.exec(
function(resp){},
function(err){},
"GCMPushPlugin",
"subscribeToTopic",
[topic])
},
unsubscribeToTopic :function(successCB,errorCB, topic){
cordova.exec(
function(resp){},
function(err){},
"GCMPushPlugin",
"unsubscribeToTopic",
[topic]
)
}
};
module.exports = GcmPushPlugin;