我正在IOS客户端上实现GCM。一切都很顺利,直到我尝试从IOS设备向GCM发送消息。
-
我使用
取回GCM令牌GLInstanceID.sharedInstance().startWithConfig(GGLInstanceIDConfig.defaultConfig())
-
我配置上下文
GGLContext.sharedInstance().configureWithError(&configureError)
-
我配置GCMService委托并启动服务
var config = GCMConfig.defaultConfig() config.receiverDelegate = self GCMService.sharedInstance().startWithConfig(config)
-
我连接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() }
订阅成功
当我尝试发送消息时,我收到一个未经授权的错误。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并给出解释就好了。