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 Dec 15, 2015
1 parent 0f1fba0 commit bdd12d4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 12 deletions.
1 change: 0 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
17 changes: 10 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 Down Expand Up @@ -66,5 +67,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 @@ -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,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 @@ -93,6 +158,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 +409,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 @@ -523,4 +595,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 bdd12d4

Please sign in to comment.