9

我正嘗試訂閱iOS上的gcm主題。Gcm iOS,訂閱主題,錯誤代碼3004

GCMPubSub.sharedInstance().subscribeWithToken(registrationId, topic: "/topics/mytopic", options: nil) { error in 
    print(error.localizedDescription) 
} 

操作無法完成。 (com.google.gcm error 3004.)

我無法在任何地方找到此錯誤代碼的文檔。我也讀過錯誤定義的源代碼,它看起來像這樣:

typedef NS_ENUM(NSUInteger, GCMServiceErrorCode) { 
    /** 
    * HTTP errors. 
    */ 

    // InvalidRequest -- Some parameters of the request were invalid. 
    kGCMServiceErrorCodeInvalidRequest = 0, 

    // Auth Error -- GCM couldn't validate request from this client. 
    kGCMServiceErrorCodeAuthentication = 1, 

    // NoAccess -- InstanceID service cannot be accessed. 
    kGCMServiceErrorCodeNoAccess = 2, 

    // Timeout -- Request to InstanceID backend timed out. 
    kGCMServiceErrorCodeTimeout = 3, 

    // Network -- No network available to reach the servers. 
    kGCMServiceErrorCodeNetwork = 4, 

    // OperationInProgress -- Another similar operation in progress, 
    // bailing this one. 
    kGCMServiceErrorCodeOperationInProgress = 5, 

    // Unknown error. 
    kGCMServiceErrorCodeUnknown = 7, 

    /** 
    * Upstream Send errors 
    */ 

    // Upstream send not available (e.g. network issues) 
    kGCMServiceErrorCodeUpstreamServiceNotAvailable = 1001, 

    // Invalid send parameters. 
    kGCMServiceErrorCodeInvalidParameters = 1002, 

    // Invalid missing to. 
    kGCMServiceErrorCodeMissingTo = 1003, 

    // GCM could not cache the message for sending. 
    kGCMServiceErrorSave = 1004, 

    // Message size exceeded (size > 4KB). 
    kGCMServiceErrorSizeExceeded = 1005, 

    /** 
    * GCM Connect errors. 
    */ 

    // GCM already connected with the client. 
    kGCMServiceErrorCodeAlreadyConnected = 2001, 

    /** 
    * PubSub errors. 
    */ 

    // Topic already subscribed to. 
    kGCMServiceErrorCodePubSubAlreadySubscribed = 3001, 

    // Topic already unsubscribed from. 
    kGCMServiceErrorCodePubSubAlreadyUnsubscribed = 3002, 

    // Invalid topic name, does not match the topic regex "/topics/[a-zA-Z0-9-_.~%]+" 
    kGCMServiceErrorCodePubSubInvalidTopic = 3003, 
}; 

錯誤代碼以3003結尾!

回答

6

我以前見過這個,因爲我在使用GCMPubSub之前沒有啓動GCM。因此,這應該修復它爲你

var config = GCMConfig.defaultConfig() 
// Note you should only call start once during the lifetime of your app. 
GCMService.sharedInstance().startWithConfig(config) 
GCMPubSub.sharedInstance().subscribeWithToken(registrationId, topic: "/topics/mytopic", options: nil) { error in 
    print(error.localizedDescription) 
} 
+0

那是以前存儲GCMPubSub.shareInstance()作爲財產的問題就是我錯過。非常感謝! – Siamaster

+0

我也有這個問題,這並沒有解決我的錯誤代碼3004 – oronbz

+0

謝謝!注意不注意'GCMService'和'GGLInstanceID'的人:兩者不同! – wangii

1

對我來說,你甚至不能稱之爲 GCMPubSub.sharedInstance()直到調用GCMService.sharedInstance().startWithConfig(config) 所以你不能實際調用subscribeWithToken