How to sign up for GCM on iOS
I cannot get GCM push notifications to work. My problem is that I don't know how to get the registration ID from GCM. I can get the token from APN just fine. But I'm not really sure what to do next. I tried following the tutorial but it doesn't work for me. I am a beginner, so please be clear.
What I ask is, after receiving a token from APN, what should I do?
Thanks in advance. https://developers.google.com/cloud-messaging/ios/client
source to share
Registration token is provided to the registration handler from didRegisterForRemoteNotificationsWithDeviceToken
All the codes below are taken from the GCM example from Google.
First, declare a handler in your application: didFinishLaunchingWithOptions:
_registrationHandler = ^(NSString *registrationToken, NSError *error){
if (registrationToken != nil) {
weakSelf.registrationToken = registrationToken;
NSLog(@"Registration Token: %@", registrationToken);
NSDictionary *userInfo = @{@"registrationToken":registrationToken};
[[NSNotificationCenter defaultCenter] postNotificationName:weakSelf.registrationKey
object:nil
userInfo:userInfo];
} else {
NSLog(@"Registration to GCM failed with error: %@", error.localizedDescription);
NSDictionary *userInfo = @{@"error":error.localizedDescription};
[[NSNotificationCenter defaultCenter] postNotificationName:weakSelf.registrationKey
object:nil
userInfo:userInfo];
}
};
Calling the handler in the application registration callback:
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Start the GGLInstanceID shared instance with the default config and request a registration
// token to enable reception of notifications
[[GGLInstanceID sharedInstance] startWithConfig:[GGLInstanceIDConfig defaultConfig]];
_registrationOptions = @{kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:@YES};
[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:_gcmSenderID
scope:kGGLInstanceIDScopeGCM
options:_registrationOptions
handler:_registrationHandler];
}
The GCM token used is simply "NSString * registrationToken" in the registration handler.
source to share