[英]iOS GCM error code 501
最近,当我尝试在我的iOS应用程序中使用GCM apis时,我开始收到此错误代码:Error Domain = com.google.gcm Code = 501“(null)”
我无法在任何地方找到这个意义吗? 这实际上是HTTP状态代码,意味着没有实现?
我在这行代码中首先得到错误:
GCMService.sharedInstance().connectWithHandler() { error in if(error != nil) { print(error) } }
调用该方法打印此消息:
GCM | GCM注册尚未准备好使用身份验证凭据
错误是错误域= com.google.gcm代码= 501“(null)”
发生错误是因为我正在打电话
GCMService.sharedInstance().connectWithHandler() { error in if(error != nil) { print(error) } }
在我收到注册令牌或未能刷新令牌之前。
“Error Domain = com.google.gcm Code = 501”(null)“”是一个非常糟糕的错误消息。
如果你一直在使用这个样本开发
https://github.com/googlesamples/google-services/blob/master/ios/gcm/GcmExample/
比你必须知道的,在某些方面它是非常糟糕和错误的
修复上面的错误(你确实成功请求[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity
,但仍然弹出错误),你必须调用ALSO [[GGLInstanceID sharedInstance] startWithConfig:
在调用[[GCMService sharedInstance] connectWithHandler:
- (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];
// add this
{
GGLInstanceIDConfig *instanceIDConfig = [GGLInstanceIDConfig defaultConfig];
instanceIDConfig.delegate = self;
[[GGLInstanceID sharedInstance] startWithConfig:instanceIDConfig];
}
...
[self requestAndSynchronizeGCMTokenIfNeeded];
...
return YES;
}
但请记住,当[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:
未完成时,您将始终收到错误,因此您应该在applicationDidBecomeActive
smth中添加一个chcek。 喜欢
- (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:
的处理程序块tokenWithAuthorizedEntity:
因为它有时在令牌被重新接收之前被调用,因此以错误结束
[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:gcmSenderID scope:kGGLInstanceIDScopeGCM options:options handler:^(NSString *token, NSError *error) {
// connect to GCM also here ([[GCMService sharedInstance] connectWithHandler:)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.