iOS GCM error code 501

前端 未结 2 1202
一向
一向 2020-12-11 21:29

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 meanin

相关标签:
2条回答
  • 2020-12-11 22:07

    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:)
    }
    
    0 讨论(0)
  • 2020-12-11 22:08

    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.

    0 讨论(0)
提交回复
热议问题