我正在为iOS客户端实现GCM。 一切都进行得很顺利,直到我尝试从iOS设备向GCM发送消息。
I get back the GCM token using
GLInstanceID.sharedInstance().startWithConfig(GGLInstanceIDConfig.defaultConfig())
I configure the context
GGLContext.sharedInstance().configureWithError(&configureError)
I configure the GCMService delegate and start the service
var config = GCMConfig.defaultConfig() config.receiverDelegate = self GCMService.sharedInstance().startWithConfig(config)
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 中被调用。
I subscribe successfully to a topic.
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