如何在iOS中的GCM注册 [英] How to register for GCM in iOS
问题描述
我似乎无法得到GCM推送通知的工作。我的问题是我不知道如何从GCM获得一个注册ID。我可以从APN令牌就好了。但我不太清楚下一步该怎么做。我试图按照教程,但它不是一个真正为我工作。我是一个初学者,所以请明确。
I can't seem to get GCM push notifications working. My problem is I don't know how to get a registration ID from GCM. I can get a token from APN just fine. But I'm not quite sure what to do next. I tried following the tutorial but its not really working for me. I'm a beginner so please be explicit.
什么我问的是,从获取APN令牌后,再我该怎么办?
What I'm asking is, after obtaining a token from APN, then what do I do?
在此先感谢。
https://developers.google.com/cloud-messaging/ios/client
推荐答案
的注册凭证提供给注册的处理程序从didRegisterForRemoteNotificationsWithDeviceToken
The Registration Token is given to the registration handler from didRegisterForRemoteNotificationsWithDeviceToken
所有code下面是从谷歌的GCM取试样。
All code below is taken from the GCM Sample from Google.
首先,在应用程序中声明的处理程序中:didFinishLaunchingWithOptions:
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];
}
};
调用处理中的应用程序注册的回调:
Call your 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];
}
的GCM令牌使用简称为* NSString的registrationToken中的登记处理。
The GCM token to use is simply the "NSString *registrationToken" in the registration handler.
这篇关于如何在iOS中的GCM注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!