Push Notifications in Rhodes using Ruby

Surendran Sukumaran

1 min read

I tried Push notifications concept in Rhodes using Rhoconnect and its works well. The documentation for the same is also nice.
For one of my project there was no need for Rhoconnect and so I wanted to get Push Notification done using Ruby (without Rhoconnect). Here is the sample code I tried.
Create a simple Rhodes app using rhodes app app_name

  • Step 1 :
    Enable push by adding it in build.yml file under capablities.
    [source language=”ruby”]
    capablities:
    – push
    [/source]
  • Step 2:
    Add the registered emailid c2dm_registered@gmail.com under the android section in build.yml file
    [source language=”ruby”]
    android
    – push
    – email : c2dm_registered@gmail.com
    [/source]
  • Step 3:
    Find the registeration id or device id (device id registered to receive push Notification for this particular app)
    [source language=”ruby”]
    System.get(‘device_id’) # => A12A13123wiu13-erughyiug3
    [/source]
    Which returns a long string, this is the registration id which we will be using to send a push notification to the actual mobile device (Warning “it wont work with Emulators).
  • Step 4:
    Handle what should be done when we receive a Push Notification
    Set a handler at app/application.rb
    [source language=”ruby”]
    System.get_push_notification “/app/Push/receive_push”, “params to be passed”
    [/source]
  • Step 5:
    Implement the action receive_push.
    So whenever the Device App receive’s a Notification the “receive_push” method will be called.

The platform I have chosen is Android devices.

Note : You should be registered with Google C2DM with your gmail id in the URL(https://code.google.com/android/c2dm/signup.html) by which you can Send and Receive Push Notifications.
You will be receving an email from the Google team after registration.
Push works with Android 2.2 or greater versions.

That’s it. Everything is set so far and now the device is ready to receive Push Notifications. 🙂
Now the important part is to send the push notification messages from our server side using ruby code.
I used sepeedy_c2dm ruby client for the same.
Please see the sample code implementation below

Lets run it all together.
When you execute this Ruby code, the device should call the “receive_push” method.
Hurdles I faced while trying Push.
1) Device mismatchID
When there is a mismatch in the email id registred with C2DM in the server side and the mobile(client) side, the server will throw error saying devicemismathcid.
2) If you forget to add push under the capablities, the System.get(“device_id”) will return an empty string or nil value.
3) If the api (google android version 2.2+) is not properly installed, it may thrown an error.
Thanks to Rhomobile team for making my life easier with the Push Notifications from the Rhoconnect perspective.

Related posts:

3 Replies to “Push Notifications in Rhodes using Ruby”

  1. You can use c2dm gem if you want send standalone push notifications to android and APNS gem for iOS.. For iOS you have to use a developer certificate(which will be .pem file).

  2. Is this all that needs to happen for at least the device_id in iPhone? I’ve been trying to just add capabilities push to build.yml but the device_id is still null and I’m not prompted to allow for notifications.

  3. Does your module work with the neswet release of ActiveCollab? And does it have the ability to only send when a user has been added to a project, as apposed to the individual tasks?

Leave a Reply

Your email address will not be published. Required fields are marked *