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

Commit

Permalink
Issue #79: Subscribe to Topics for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Jan 11, 2016
1 parent 350efbe commit 8a8909e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 13 deletions.
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 8a8909e

Please sign in to comment.