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



我正在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()
    }
    
此时此刻,我遇到了一个问题。completionHandler永远不会被调用。甚至尝试GCM示例应用程序也给了我相同的结果。目前,我在completionHandler外部调用sendRegistrationInfo(),但它应该在没有错误时在completionHandler中调用。
  • 订阅成功

  • 当我尝试发送消息时,我收到一个未经授权的错误。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)")
    }
    
  • 我在willSendDataMessageWithID中从GCM收到的错误是"操作无法完成。(com.google。GCM误差1。"

    我猜GCMService.sharedInstance()有问题。connectWithHandler,但我不知道。我设置的唯一东西是GCM项目ID。

    感谢您的帮助或见解!

    迈克尔

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

    但是,如果调用completionHandler并给出解释就好了。

    最新更新