How to Implement Android Notifications in Rails
Mobile notifications comprise a great feature for your app - especially when you integrate mobile and desktop solutions.
Here's a quick guide on how to set up Android notifications in your Ruby on Rails project.
I assume you have some kind of user_id present in your app, ex. you are using Devise gem.
What You Will Use:
- https://github.com/spacialdb/gcm - a simple wrapper around Google Cloud Messaging API (GCM),
- android project ID and API key - can be obtained here,
- some android app that registers to GCM and sends registration id to backend API.
- really simple to implement - no magic,
- small library - 179 lines of code,
- makes use of Google Cloud Messaging,
- can be used both for Android and iOS (we tested only with Android).
- Google Cloud Messaging might be not viable for you if you require very low latency - see Messenger case study.
Add to Gemfile:
And run bundle install.
Add API key to sec-config.yml:
Now, we can make use of GCM and send some notifications. In the snippet below, you can find some example service object that takes registration IDs (that need to be pushed by mobile clients to your backend API) and a message hash which is just a message that will be included in the notification.
Android clients need to listen to those notifications by using the broadcast receiver. What it will do with those notifications will depend on the business logic of your application. A sample code for such an application can be found here.
I hope you like it! If you have any comments or ideas how to improve it, please share them below.