GCM服务不从IOS客户端发送消息

时间:2015-08-13 09:44:36

标签: ios google-cloud-messaging

我在IOS客户端上实施GCM。 一切顺利,直到我尝试从IOS设备向GCM发送消息。

  1. 我使用

    取回GCM令牌
    GLInstanceID.sharedInstance().startWithConfig(GGLInstanceIDConfig.defaultConfig())
    
  2. 我配置上下文

    GGLContext.sharedInstance().configureWithError(&configureError)
    
  3. 我配置GCMService委托并启动服务

    var config = GCMConfig.defaultConfig()
    config.receiverDelegate = self
    GCMService.sharedInstance().startWithConfig(config)
    
  4. 我连接GCMService

    var completionHandler: GCMServiceConnectCompletion = { (error: NSError!) in
        if ((error) != nil) {
            println("Could not connect to GCM Service: \(error.localizedDescription)")
            self.connectedToGCM = false
        } else {
            self.connectedToGCM = true
            println("Connected to GCM Service")
        }
    }
    
    GCMService.sharedInstance().connectWithHandler(completionHandler)
    self.subscribeToTopic(currentUser!.gcmToken)
    if self.shouldSendRegistrationInfo {
        self.sendRegistrationInfo()
    }
    
  5. 此时此刻,我有一个问题。从未调用completionHandler。 即使尝试GCM示例应用程序也给了我相同的结果。 目前我在completionHandler之外调用sendRegistrationInfo(),但是当没有错误时应该在completionHandler中调用它。

    1. 我成功订阅了一个主题。

    2. 当我尝试发送邮件时,收到未经授权的错误。     var nextMessageID = 1

      var message = [
          "user": "michael",
          "hello": "world"
      ]
      
      println("dico : \(message)")
      var to = "\(self.gcmSenderID!)@gcm.googleapis.com"
      GCMService.sharedInstance().sendMessage(message, to: to, withId: "\(nextMessageID)")
      
      
      }
      
      func willSendDataMessageWithID(messageID: String, error: NSError){
          println("willSendDataMessageWithID - \(messageID)")
          println("error: \(error.localizedDescription)")
      }
      
    3. 我在willSendDataMessageWithID中从GCM收到的错误是 "操作无法完成。 (com.google.gcm错误1。)"

      我的猜测是GCMService.sharedInstance()。connectWithHandler出了问题,但我不知道是什么。我设置的唯一内容是GCM项目ID。

      感谢您的帮助或见解!

      迈克尔

1 个答案:

答案 0 :(得分:0)

我发现:我使用的wifi连接阻止了一些端口。 通过使用3G连接,GCMService.sharedInstance().connectWithHandler(completionHandler)调用了completionHandler。

但是,通过解释调用completionHandler应该会很好。

相关问题