iOS GCM 错误代码 501

iOS GCM error code 501

最近,每当我尝试在我的 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)"

发生错误是因为我正在调用

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 中为此添加一个检查。喜欢

- (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:)
}