GCM服务无法从iOS客户端发送消息

3

我正在为iOS客户端实现GCM。 一切都进行得很顺利,直到我尝试从iOS设备向GCM发送消息。

  1. I get back the GCM token using

    GLInstanceID.sharedInstance().startWithConfig(GGLInstanceIDConfig.defaultConfig())
    
  2. I configure the context

    GGLContext.sharedInstance().configureWithError(&configureError)
    
  3. I configure the GCMService delegate and start the service

    var config = GCMConfig.defaultConfig()
    config.receiverDelegate = self
    GCMService.sharedInstance().startWithConfig(config)
    
  4. I connect the 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 中被调用。

  1. I subscribe successfully to a topic.

  2. When I try to send a message, I receive an unauthorized error. 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 error 1.)"

我的猜测是GCMService.sharedInstance().connectWithHandler方法存在问题,但我不知道具体是什么问题。我只设置了GCM项目ID。

谢谢您的帮助和见解!

Michael

1个回答

0
我发现:我使用的wifi连接阻止了一些端口。 通过使用3G连接,GCMService.sharedInstance().connectWithHandler(completionHandler)调用了completionHandler。
然而,最好能够通过一个解释来调用completionHandler。

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