简体   繁体   中英

iOS GCM error code 501

Recently I start to get this error code whenever I try to use the GCM apis in my iOS app: Error Domain=com.google.gcm Code=501 "(null)"

I couldn't find the meaning of this anywhere? Is that actually the HTTP-status code, meaning Not Implemented?

I'm getting the error first at this line of code:

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

calling the method prints this message :

GCM | GCM registration is not ready with auth credentials

and the error is Error Domain=com.google.gcm Code=501 "(null)"

The error occurred because I was calling

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

before I had received a registration token, or had failed to refresh my token.

"Error Domain=com.google.gcm Code=501 "(null)" " is a really bad error message.

if you are been developing using this sample

https://github.com/googlesamples/google-services/blob/master/ios/gcm/GcmExample/

than you have to know, it is quite poor and wrong in some points

to fix the error above (you did request the [[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity successfully, but still that error pops up), you have to call ALSO [[GGLInstanceID sharedInstance] startWithConfig: before calling the [[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;
}

BUT remember, you will always get the error, when the [[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity: did not complete, so you should add a chcek for that in the applicationDidBecomeActive smth. like

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

            }

        }];

    }

}

and the last thing, you should try to connect in the handler block of tokenWithAuthorizedEntity: as it is sometimes called before the token is recaived and therefore ending in error

[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:gcmSenderID scope:kGGLInstanceIDScopeGCM options:options handler:^(NSString *token, NSError *error) {
  // connect to GCM also here ([[GCMService sharedInstance] connectWithHandler:)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM