如果您一直在使用这个示例进行开发
https://github.com/googlesamples/google-services/blob/master/ios/gcm/GcmExample/
如果你想要解决上述错误(虽然你已经成功请求了[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity
],但仍然出现错误),你需要在调用[[GCMService sharedInstance] connectWithHandler:
]之前,也要调用[[GGLInstanceID sharedInstance] startWithConfig:
。
需要注意的是,有些地方的代码可能存在问题或错误。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSError* configureError;
[[GGLContext sharedInstance] configureWithError:&configureError];
if (configureError) {
NSLog(@"Error configuring Google services: %@", configureError);
}
GCMConfig *gcmConfig = [GCMConfig defaultConfig];
gcmConfig.receiverDelegate = self;
[[GCMService sharedInstance] startWithConfig:gcmConfig];
{
GGLInstanceIDConfig *instanceIDConfig = [GGLInstanceIDConfig defaultConfig];
instanceIDConfig.delegate = self;
[[GGLInstanceID sharedInstance] startWithConfig:instanceIDConfig];
}
...
[self requestAndSynchronizeGCMTokenIfNeeded];
...
return YES;
}
但是请记住,当[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:
未完成时,您将始终收到错误提示,因此您应该在applicationDidBecomeActive
中添加一个检查,例如:
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (self.data.deviceToken) {
[[GCMService sharedInstance] connectWithHandler:^(NSError *error) {
if (error) {
NSLog(@"Could not connect to GCM: %@", error.localizedDescription);
} else {
self.connectedToGCM = YES;
NSLog(@"Connected to GCM");
[self subscribeToTopic];
}
}];
}
}
最后一件事,你应该尝试在tokenWithAuthorizedEntity:
的处理程序块中连接,因为有时它被称为在令牌接收之前调用,因此可能会导致错误。
[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:gcmSenderID scope:kGGLInstanceIDScopeGCM options:options handler:^(NSString *token, NSError *error) {
// connect to GCM also here ([[GCMService sharedInstance] connectWithHandler:)
}