All Case Studies Design Development Interviews Our Way Project Management

How To Implement Android Notifications In Rails

Mobile notifications are great feature for your app, especially when you integrate mobile and desktop solutions. Here's a quick guide how to setup Android notifications in your Ruby on Rails project.

 

I am assuming 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 - 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

Pros

  • Really simple to implement - no magic
  • Small library - 179 lines of code
  • It makes use of Google Cloud Messaging
  • Can be used both for Android and iOS (we tested only with Android)

Cons

  • Google Cloud Messaging might be not viable for you if you require very low latency - see Messenger case study

 

Step 1

Add to Gemfile:

gem ‘gcm’

And run bundle install

 

Step 2

Add API key to sec-config.yml:

environment:
  google_gcm_api: some1cool1key1here

 

Step 3

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 message hash which is just message that will be included in notification.

[snippet]

Step 4

Android client has to be listening to those notifications by using broadcast receiver. What it will do with those notifications will depend on business logic of your application. Sample code for such 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.

Follow Netguru
Join our Newsletter

READ ALSO FROM Mobile
Read also
Need a successful project?
Estimate project or contact us