iOS GCM错误代码501

4

最近我在iOS应用中使用GCM API时,每次都会出现这个错误代码:Error Domain=com.google.gcm Code=501“(null)”

我找不到这个错误的含义?这实际上是HTTP状态码,意味着未实施吗?

我在以下代码行首先遇到了这个错误:

        GCMService.sharedInstance().connectWithHandler() { error in  if(error != nil) {   print(error) } }

调用该方法会打印出以下信息:

GCM | GCM注册凭证尚未准备好

错误代码为Error Domain=com.google.gcm Code=501 "(null)"


谷歌文档将其标识为内部错误:https://developers.google.com/cloud-messaging/http-server-ref#interpret-downstream - Guga
嗯,好的。不知道为什么我突然开始收到这个消息,因为我没有更改任何相关代码。已经两天了。 - Siamaster
@Siamaster,您尝试重置设备并查看是否仍然出现了吗? - evanescent
1
我找到了问题,我自己回答了这个问题。谢谢帮助。 - Siamaster
我在didFinishLaunchingWithOptions中调用它。我认为有时候你需要刷新你的注册令牌,直到你刷新令牌之前,你会收到这个错误。尝试刷新你的注册令牌并查看是否有效。 - Siamaster
显示剩余2条评论
2个回答

3
错误发生的原因是我调用了
GCMService.sharedInstance().connectWithHandler() { error in  if(error != nil) {   print(error) } } 

在我收到注册令牌之前,或者未能刷新我的令牌之前。

"Error Domain=com.google.gcm Code=501 "(null)" "是一个非常糟糕的错误信息。


1
什么是理想的解决方案? - Nabeel Ahmed
我觉得很奇怪,因为即使[[GGLContext sharedInstance] registrationToken]有时候是nil,在连接时也没有产生任何错误。 - Peter Lapisu

0

如果您一直在使用这个示例进行开发

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];

    // add this
    {
        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:)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接