Skip to content

Commit

Permalink
Fix example of GCM custom delegates
Browse files Browse the repository at this point in the history
Add example for GCMReceiverDelegate
Fix setting delegate on config
  • Loading branch information
silvolu committed Aug 12, 2015
1 parent 6f138a6 commit f6e2ea1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ios/gcm/GcmExample/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#import <Google/CloudMessaging.h>
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, GGLInstanceIDDelegate>
@interface AppDelegate : UIResponder <UIApplicationDelegate, GGLInstanceIDDelegate, GCMReceiverDelegate>

@property(nonatomic, strong) UIWindow *window;
@property(nonatomic, readonly, strong) NSString *registrationKey;
Expand Down
31 changes: 28 additions & 3 deletions ios/gcm/GcmExample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ - (BOOL)application:(UIApplication *)application
}
// [END register_for_remote_notifications]
// [START start_gcm_service]
[[GCMService sharedInstance] startWithConfig:[GCMConfig defaultConfig]];
GCMConfig *gcmConfig = [GCMConfig defaultConfig];
gcmConfig.receiverDelegate = self;
[[GCMService sharedInstance] startWithConfig:gcmConfig];
// [END start_gcm_service]
__weak typeof(self) weakSelf = self;
// Handler for registration token request
Expand Down Expand Up @@ -138,9 +140,12 @@ - (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// [END receive_apns_token]
// [START get_gcm_reg_token]
// Start the GGLInstanceID shared instance with the default config and request a registration
// Create a config and set a delegate that implements the GGLInstaceIDDelegate protocol.
GGLInstanceIDConfig *instanceIDConfig = [GGLInstanceIDConfig defaultConfig];
instanceIDConfig.delegate = self;
// Start the GGLInstanceID shared instance with the that config and request a registration
// token to enable reception of notifications
[[GGLInstanceID sharedInstance] startWithConfig:[GGLInstanceIDConfig defaultConfig]];
[[GGLInstanceID sharedInstance] startWithConfig:instanceIDConfig];
_registrationOptions = @{kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:@YES};
[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:_gcmSenderID
Expand Down Expand Up @@ -203,4 +208,24 @@ - (void)onTokenRefresh {
}
// [END on_token_refresh]

// [START receiver_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
}

- (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.
}
// [END receiver_callbacks]

@end
33 changes: 29 additions & 4 deletions ios/gcm/GcmExampleSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate {
class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate, GCMReceiverDelegate {

var window: UIWindow?

Expand Down Expand Up @@ -52,7 +52,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate {
application.registerForRemoteNotifications()
// [END register_for_remote_notifications]
// [START start_gcm_service]
GCMService.sharedInstance().startWithConfig(GCMConfig.defaultConfig())
var gcmConfig = GCMConfig.defaultConfig()
gcmConfig.receiverDelegate = self
GCMService.sharedInstance().startWithConfig(gcmConfig)
// [END start_gcm_service]
return true
}
Expand Down Expand Up @@ -110,9 +112,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate {
deviceToken: NSData ) {
// [END receive_apns_token]
// [START get_gcm_reg_token]
// Start the GGLInstanceID shared instance with the default config and request a registration
// Create a config and set a delegate that implements the GGLInstaceIDDelegate protocol.
var instanceIDConfig = GGLInstanceIDConfig.defaultConfig()
instanceIDConfig.delegate = self
// Start the GGLInstanceID shared instance with that config and request a registration
// token to enable reception of notifications
GGLInstanceID.sharedInstance().startWithConfig(GGLInstanceIDConfig.defaultConfig())
GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig)
registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:true]
GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,
Expand Down Expand Up @@ -184,4 +189,24 @@ class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate {
}
// [END on_token_refresh]

// [START receiver_callbacks]
func willSendDataMessageWithID(messageID: String!, error: NSError!) {
if (error != nil) {
// Failed to send the message.
} else {
// Will send message, you can save the messageID to track the message
}
}

func didSendDataMessageWithID(messageID: String!) {
// Did successfully send message identified by messageID
}

func 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.
}
// [END receiver_callbacks]

}

0 comments on commit f6e2ea1

Please sign in to comment.