How to Implement Android Notifications in Rails

Photo of Bartosz Łęcki

Bartosz Łęcki

May 4, 2016 • 2 min read

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.

Pros:

  • 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).

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

[snippet]

Step 4

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.

Photo of Bartosz Łęcki

More posts by this author

Bartosz Łęcki

A student of Computer Science at Adam Mickiewicz University in Poznań. Bartosz got his very first...
How to build products fast?  We've just answered the question in our Digital Acceleration Editorial  Sign up to get access

We're Netguru!

At Netguru we specialize in designing, building, shipping and scaling beautiful, usable products with blazing-fast efficiency
Let's talk business!

Trusted by:

  • Vector-5
  • Babbel logo
  • Merc logo
  • Ikea logo
  • Volkswagen logo
  • UBS_Home