Created
March 22, 2016 02:59
-
-
Save CavalcanteLeo/ca51ef40663d95eb1e5c to your computer and use it in GitHub Desktop.
GCM implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface AppDelegate() <GGLInstanceIDDelegate, GCMReceiverDelegate> | |
@property(nonatomic, strong) NSString *gcmSenderID; | |
@property(nonatomic, strong) NSDictionary *registrationOptions; | |
@end | |
@implementation AppDelegate | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | |
[self gcmHelperSetup]; | |
return YES; | |
} | |
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { | |
GGLInstanceIDConfig *instanceIDConfig = [GGLInstanceIDConfig defaultConfig]; | |
instanceIDConfig.delegate = self; | |
[[GGLInstanceID sharedInstance] startWithConfig:instanceIDConfig]; | |
self.registrationOptions = @{kGGLInstanceIDRegisterAPNSOption:deviceToken, | |
kGGLInstanceIDAPNSServerTypeSandboxOption:@NO}; | |
[self gcmHelperToken]; | |
} | |
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { | |
NSLog(@"Registration for remote notification failed with error: %@", error.localizedDescription); | |
} | |
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { | |
NSLog(@"Notification received: %@", userInfo); | |
[[GCMService sharedInstance] appDidReceiveMessage:userInfo]; | |
} | |
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler { | |
NSLog(@"Notification received: %@", userInfo); | |
[[GCMService sharedInstance] appDidReceiveMessage:userInfo]; | |
handler(UIBackgroundFetchResultNoData); | |
} | |
- (void)onTokenRefresh { | |
NSLog(@"The GCM registration token needs to be changed."); | |
[self gcmHelperToken]; | |
} | |
- (void)gcmHelperToken { | |
[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:self.gcmSenderID | |
scope:kGGLInstanceIDScopeGCM | |
options:self.registrationOptions | |
handler:^(NSString *token, NSError *error) { | |
if (error) { | |
NSLog(@"Error %@", error.localizedDescription); | |
} else { | |
NSLog(@"GCM Token = %@", token); | |
} | |
}]; | |
} | |
- (void)gcmHelperSetup { | |
NSError *configureError; | |
[[GGLContext sharedInstance] configureWithError:&configureError]; | |
if (configureError) { | |
NSLog(@"GGLContext error %@", configureError); | |
} | |
self.gcmSenderID = [[[GGLContext sharedInstance] configuration] gcmSenderID]; | |
UIUserNotificationType allNotificationTypes = | |
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); | |
UIUserNotificationSettings *settings = | |
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; | |
[[UIApplication sharedApplication] registerUserNotificationSettings:settings]; | |
[[UIApplication sharedApplication] registerForRemoteNotifications]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment