A Rails API for sending Google Android push notifications
Pull request Compare This branch is 33 commits ahead, 13 commits behind pimeys:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


NOTICE: This fork should work with Rails 3 and Ruby 1.9, but has not been well tested.

C2dm on Rails (Android push notifications on Rails)

C2dm on Rails is a Ruby on Rails gem that allows you to easily add Android push notification
(Android 2.2 or higher devices) to your Rails application.


This gem is a re-write of a apn_on_rails gem that was written by Mark Bates and
before he made it a gem, it was started by Fabien Penso and Sam Soffes. The gem
installation, migration generation and usage is copied from the original apn_on_rails


First it is necessary to sign up to Android Cloud to Device Messaging service.
You need a role account email, it’s password and the name of your Android app for
configuring the c2dm gem.

Sign up here:



Stable (RubyForge):

  $ sudo gem install c2dm_on_rails

Rails Gem Management:

If you like to use the built in Rails gem management:

  config.gem 'c2dm_on_rails'

Setup and Configuration:

Once you have the gem installed via your favorite gem installation, you need to require it so you can
start to use it:

Add the following require, wherever it makes sense to you:

  require 'c2dm_on_rails'

You also need to add the following to your Rakefile so you can use the
Rake tasks that ship with C2dm on Rails:

    require 'c2dm_on_rails_tasks'
  rescue MissingSourceFile => e
    puts e.message

C2dm on Rails uses also the gdata gem for connecting a Google account.


More information about C2dm services should be read from http://code.google.com/android/c2dm/

  $ ./script/console
  >> device = C2dm::Device.create(:registration_id => "XXXXXXXXXXXXXXXXXXXXXX")
  >> notification = C2dm::Notification.new
  >> notification.device = device
  >> notification.collapse_key = "private_message"
  >> notification.delay_while_idle = true
  >> notification.data = {"sender_id" => "420", "message_text" => "Wanna go for a ride?"}
  >> notification.save

You can use the following Rake task to deliver your notifications:

  $ rake c2dm:notifications:deliver

The Rake task will find any unsent notifications in the database. If there aren’t any notifications
it will simply do nothing. If there are notifications waiting to be delivered it will login with the provided
login data and send notifications using HTTP POST to Google. The client can get an error from Google. There are
several possibilities:

code 200  
Error: QuotaExceeded. It will cancel the notification sending and user should try to send them again after a while.
Error: DeviceQuotaExceeded. It will cancel the notification sending for the current device and continue from the other devices.
  Error: InvalidRegistration. The devices registration_id is missing or invalid. The device and all its notifications will be deleted.
Error: NotRegistred. The registration_id is no longer valid. The device and all its notifications will be deleted.
Error: MessageTooBig. The maximum size of a c2dm push notification is 1024 bytes. User should reduce the size and try again.
  Error: MissingCollapseKey. Google uses a property called collapse_key to collapse a group of like messages when the device is online, so that only the last message is sent to the client. It is required.
code 503  
  The server is currently unavailable. The sending process is stopped and sender must retry later. Senders that retry too often and too fast risk being blacklisted.
code 401  
The ClientLogin auth is invalid. Check the config file.

Released under the MIT license.