简体   繁体   中英

Gcm iOS, subscribe to topic, error code 3004

I'm trying to subscribe to a gcm topic on iOS.

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

The operation couldn't be completed. (com.google.gcm error 3004.)

I can't find a documentation for this error code anywhere. I've also read the source code where the errors are defined and it looks like this:

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,
};

error codes end at 3003!

I have seen this before because I didn't start GCM before using GCMPubSub . So this should fix it for you

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

对我来说问题是你甚至不能调用GCMPubSub.sharedInstance()直到你调用GCMService.sharedInstance().startWithConfig(config)所以你不能在实际调用subscribeWithToken之前将GCMPubSub.shareInstance()存储为属性

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