如何在iOS中注册GCM

我似乎无法得到GCM推送通知的工作。 我的问题是我不知道如何从GCM获得注册ID。 我可以从APN获得一个令牌。 但我不确定接下来要做什么。 我试着按照教程,但它不是真的为我工作。 我是一个初学者,所以请明确。

我问的是,从APN获得一个令牌之后,我该怎么办?

提前致谢。 https://developers.google.com/cloud-messaging/ios/client

注册令牌从didRegisterForRemoteNotificationsWithDeviceToken被提供给注册处理程序

以下所有代码均取自Google的GCM Sample。

首先,在应用程序中声明一个处理程序: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]; } }; 

在应用程序注册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]; } 

要使用的GCM令牌只是注册处理程序中的“NSString * registrationToken”。