Android Gcm Not working with ROR

2019-07-21 04:49发布

问题:

I read basic example for implementing google gcm. I developed small client side android application which successfully gives me registration id from gcm server.. now on server side I am using Ruby on rails hence we use this gem for implementing server side of gcm https://github.com/dondeng/gcm_on_rails. I pass my registration id to to my server. I successfully ran these commands

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

used to deliver notifications:

$ rake gcm:notifications:deliver

my rake command running without any error. But after completion of all these tasks my device not receiving any message.. I don't know whats going on in background. How to confirm that sent message properly receive by device.. Need Help... Thank you....

回答1:

Debugging GCM messages can be cumbersome and difficult.

One possible reason is a wrong API key for GCM, which must be defined in the configuration files. If you use the wrong API key, then you might get a success response from the server {:code=>200, :message=>nil}, although no message has been sent. In this case go to the Google API Console and look up the right key.

Another possible reason is a wrong REGISTRATION_ID for the device. if the REGISTRATION_ID is not correct you may get an NotRegistered or InvalidRegistration error, which is not always recognized by the gcm_on_rails plugin (at the moment). Use the following curl command to debug the API through the console (insert your API_KEY and the REGISTRATION_ID of the device).

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\"}}"

A successful / unsuccessful attempt to call the Gcm API should look like this:

{"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"}]}

Finally another reason is an invalid key in the day: do not use a reserved word like message_type as a key in the data hash. It will return a status code 200, but it will not deliver any notification. message_type is one of the advanced options (took me more than an hour to find that out).



回答2:

If you have no error on server-side, maybe you are not handling correctly the event in your Android App.

Follow this guidelines: http://developer.android.com/guide/google/gcm/gs.html#android-app, and write logs for debug.