Android Gcm不使用ROR
我阅读了实现google gcm的基本示例。 我开发了小客户端android应用程序,成功地从gcm服务器给我注册ID ..现在在服务器端我使用Ruby on rails因此我们使用这个gem来实现gcm的服务器端https://github.com/dondeng/gcm_on_rails 。 我将注册ID传递给我的服务器。 我成功运行了这些命令
device = Gcm :: Device.create(:registration_id =>“XXXXXXXXXXXXXXXXXXXXXX”)
notification = Gcm :: Notification.new
notification.device = device
notification.collapse_key =“updates_available”
notification.delay_while_idle = true
notification.data = {:registration_ids => [“RegistrationID”],:data => {:message_text =>“Get on cloud nine”}}
notification.save
用于发送通知:
$ rake gcm:notifications:deliver
我的rake命令运行没有任何错误。 但在完成所有这些任务后,我的设备没有收到任何消息..我不知道在后台发生了什么。 如何确认设备正确接收发送的消息..需要帮助…谢谢….
调试GCM消息可能很麻烦且困难。
一个可能的原因是GCM的API密钥错误,必须在配置文件中定义。 如果您使用了错误的API密钥,那么您可能会从服务器{:code=>200, :message=>nil}
获得成功响应,尽管尚未发送任何消息。 在这种情况下,请转到Google API控制台并查找正确的密钥。
另一个可能的原因是设备的REGISTRATION_ID
错误。 如果REGISTRATION_ID不正确,您可能会收到NotRegistered或InvalidRegistration错误,而gcm_on_rails插件并不总能识别该错误(此刻)。 使用以下curl命令通过控制台调试API(插入API_KEY和设备的REGISTRATION_ID
)。
curl --header "Authorization: key=API_KEY" \ --header Content-Type:"application/json" \ https://android.googleapis.com/gcm/send \ -d "{\"registration_ids\":[\"REGISTRATION_ID\"], \"data\":{\"message_text\":\"Test, Test\"}}"
尝试调用Gcm API的成功/失败应如下所示:
{"multicast_id":7961..,"success":1,"failure":0,"canonical_ids":1, "results":[{"registration_id":"..","message_id":"0:.."}]} {"multicast_id":7961..,"success":0,"failure":1,"canonical_ids":0, "results":[{"error":"NotRegistered"}]}
最后另一个原因是当天无效密钥:不要使用像message_type
这样的保留字作为数据哈希中的密钥。 它将返回状态代码200,但不会发送任何通知。 message_type
是高级选项之一 (花了我一个多小时才找到它)。
如果您在服务器端没有错误,可能您没有正确处理Android应用中的事件。
请遵循以下准则: http : //developer.android.com/guide/google/gcm/gs.html#android-app ,并编写日志以进行调试。