Easy to use, full featured APNs daemon for Rails 3
Pull request Compare This branch is 11 commits ahead, 462 commits behind ileitch:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Build Status


Who uses rapns?

GateGuru, among others!

I'd love to hear if you use rapns - @ileitch on twitter.

Getting Started

Add rapns to your Gemfile:

gem 'rapns'

Generate the migration, rapns.yml and migrate:

rails g rapns
rake db:migrate

Generating Certificates

  1. Open up Keychain Access and select the Certificates category in the sidebar.

  2. Expand the disclosure arrow next to the iOS Push Services certificate you want to export.

  3. Select both the certificate and private key.

  4. Right click and select Export 2 items....

  5. Save the file as cert.p12, make sure the File Format is Personal Information Exchange (p12).

  6. If you decide to set a password for your exported certificate, please read the 'Adding Apps' section below.

  7. Convert the certificate to a .pem, where <environment> should be development or production, depending on the certificate you exported.

    openssl pkcs12 -nodes -clcerts -in cert.p12 -out <environment>.pem

Create an App

app = Rapns::App.new
app.key = "my_app"
app.certificate = File.read("/path/to/development.pem")
app.environment = "development"
app.password = "certificate password"
app.connections = 1
  • key is a symbolic name to tie this app to notifications.
  • certificate is the contents of your PEM certificate, NOT its path on disk.
  • environment the certificate type, either development or production.
  • password should be left blank if you did not password protect your certificate.
  • connections (default: 1) the number of connections to keep open to the APNs. Consider increasing this if you are sending a large number of notifications to this app.

Starting the rapns Daemon

cd /path/to/rails/app
bundle exec rapns <Rails environment> [options]


  • -f --foreground Prevent rapns from forking into a daemon.
  • -P N --db-poll N Frequency in seconds to check for new notifications. Default: 2.
  • -F N --feedback-poll N Frequency in seconds to check for feedback. Default: 60.
  • -e --no-error-checks Disables error checking after notification delivery. You may want to disable this if you are sending a very high number of notifications.
  • -n --no-airbrake-notify Disables error notifications via Airbrake.
  • -p PATH --pid-file PATH Path to write PID file. Relative to Rails root unless absolute.
  • -b N --batch-size N ActiveRecord batch size of notifications. Increase for possible higher throughput but higher memory footprint. Default: 5000.

Sending a Notification

n = Rapns::Notification.new
n.app = "my_app"
n.device_token = "934f7a..."
n.alert = "This is the message shown on the device."
n.badge = 1
n.sound = "1.aiff"
n.expiry = 1.day.to_i
n.attributes_for_device = {"question" => nil, "answer" => 42}
n.deliver_after = 1.hour.from_now
  • app must match key on an Rapns::App.
  • sound defaults to 1.aiff. You can either set it to a custom .aiff file, or nil for no sound.
  • expiry is the time in seconds the APNs (not rapns) will spend trying to deliver the notification to the device. The notification is discarded if it has not been delivered in this time. Default is 1 day.
  • attributes_for_device is the NSDictionary argument passed to your iOS app in either didFinishLaunchingWithOptions or didReceiveRemoteNotification.
  • deliver_after is not required, but may be set if you'd like to delay delivery of the notification to a specific time in the future.

Mobile Device Management

n = Rapns::Notification.new
n.mdm = "magic"

Assigning a Hash to alert

Please refer to Apple's documentation (Tables 3-1 and 3-2).

Hot App Updates

If you signal the rapns process with HUP it will synchronize with the current Rapns::App configurations. This includes adding an app, removing and increasing/decreasing the number of connections an app uses.

This synchronization process does not pause the delivery of notifications to other apps.


rapns logs activity to rapns.log in your Rails log directory. This is also printed to STDOUT when running in the foreground. When running as a daemon rapns does not print to STDOUT or STDERR.

Delivery Failures

The APNs provides two mechanism for delivery failure notification:

Immediately, when processing a notification for delivery.

Although rapns makes such errors highly unlikely due to validation, the APNs reports processing errors immediately after being sent a notification. These errors are all centred around the well-formedness of the notification payload. Should a notification be rejected due to such an error, rapns will update the following attributes on the notification and send a notification via Airbrake/Hoptoad (if enabled):

failed flag is set to true. failed_at is set to the time of failure. error is set to Apple's code for the error. error_description is set to a (somewhat brief) description of the error.

rapns will not attempt to deliver the notification again.

Via the Feedback Service.

rapns checks for feedback periodically and stores results in the Rapns::Feedback model. Each record contains the device token and a timestamp of when the APNs determined that the app no longer exists on the device.

It is your responsibility to avoid creating new notifications for devices that no longer have your app installed. rapns does not and will not check Rapns::Feedback before sending notifications.

Note: In my testing and from other reports on the Internet, it appears you may not receive feedback when using the APNs sandbox environment.

Updating rapns

After updating you should run rails g rapns to check for any new migrations.


Contributing to rapns

Fork as usual and go crazy!

When running specs, please note that the ActiveRecord adapter can be changed by setting the ADAPTER environment variable. For example: ADAPTER=postgresql rake.

Available adapters for testing are mysql, mysql2 and postgresql.


Thank you to the following wonderful people for contributing to rapns: