iOS GCM GCMPubSub取消订阅错误7 - 未知错误

3

在进行GCM主题消息订阅时,我尝试取消订阅一个主题,但是在我的日志中出现了以下内容(请参见下面的代码)。

Failed to unsubscribe from topic /topics/testTopicName: Error Domain=com.google.gcm Code=7 "The operation couldn’t be completed. (com.google.gcm error 7.)"

现在,我可以使用相同的注册令牌订阅主题,并接收有关此主题的主题消息,但我无法取消订阅。每次尝试取消订阅时,我都会收到此错误信息。根据Google,"7"是一个“未知错误”——太好了...
到目前为止,我已观察到所有我创建的主题都具有相同的行为。
+ (void)unsubscribeFromGCMTopic:(NSString*)topicName
{
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

// If the app has a registration token and is connected to GCM, proceed to unsubscribe from the
// topic
if (app.registrationToken && app.connectedToGCM)
{
    NSString* topic = [NSString stringWithFormat:@"%@%@", TOPIC_PREFIX, topicName];

    [[GCMPubSub sharedInstance] unsubscribeWithToken:app.registrationToken
                                               topic:topic
                                             options:nil
                                             handler:^void(NSError *error)
                                             {
                                                 if (error)
                                                 {
                                                     NSLog(@"Failed to unsubscribe from topic %@: %@", topic, error);                                                        
                                                 }
                                                 else
                                                 {
                                                     // Unsubscribe successfully
                                                     NSLog(@"Successfully unsubscribe from topic %@", topic);
                                                 }
                                             }];
}
else
{
    NSLog(@"Cannot GCM unsubscribe from %@. Token %@. GCM connection status %d", topicName, app.registrationToken, app.connectedToGCM);
}
}

有任何想法吗?提前感谢!


错误代码7通常意味着服务器的响应无效。即使取消订阅(尽管代码返回未成功订阅),您仍能否向主题发送下行消息? - evanescent
是的,我仍然能够为(应该被取消订阅的)主题发送下行消息。 - iht
嗯,这看起来很奇怪,但对我来说一切正常。你可能需要在失败后进行延迟重试。不确定为什么所有主题都会出现这种情况。你尝试删除应用程序并获取新的gcmToken,然后使用它进行尝试了吗? - evanescent
1个回答

0

在应用程序委托中检查是否使用沙盒或生产环境 kGGLInstanceIDAPNSServerTypeSandboxOption:true]

对于生产环境:

 //RECEBE DADOS CERTIFICADOS_APNS_TOKENS
func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken
    deviceToken: NSData ) {
        let instanceIDConfig = GGLInstanceIDConfig.defaultConfig()
        instanceIDConfig.delegate = self

        print("\nDEVICE: \(deviceToken)\n")


        //PARA TESTAR NOTIFICACAO COLOCAR SANDBOX COMO TRUE e para PRODUCAO COLOCAR COMO FALSE
        GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig)
        registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken, kGGLInstanceIDAPNSServerTypeSandboxOption:false]
        GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID, scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)

}

用 true 进行测试:

 //RECEBE DADOS CERTIFICADOS_APNS_TOKENS
func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken
    deviceToken: NSData ) {
        let instanceIDConfig = GGLInstanceIDConfig.defaultConfig()
        instanceIDConfig.delegate = self

        print("\nDEVICE: \(deviceToken)\n")


        //PARA TESTAR NOTIFICACAO COLOCAR SANDBOX COMO TRUE e para PRODUCAO COLOCAR COMO FALSE
        GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig)
        registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken, kGGLInstanceIDAPNSServerTypeSandboxOption:true]
        GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID, scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)

}

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