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.
- Step 2:
Add the registered emailid firstname.lastname@example.org under the android section in build.yml file
– email : email@example.com
- Step 3:
Find the registeration id or device id (device id registered to receive push Notification for this particular app)
System.get(‘device_id’) # => A12A13123wiu13-erughyiug3
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
System.get_push_notification “/app/Push/receive_push”, “params to be passed”
- 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.