All Case Studies Design Development Interviews Our Way Project Management

How To Implement Apple Push 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 Apple notifications in your Ruby on Rails project.


I assume you already have these:

  • Apple certificate for your application

  • Rails app or API user interface implemented

  • For production: any tool set for managing background jobs


What you will use:

  • Houston - gem for sending push notifications

  • iPhone or iPad to test your notifications


  • Easy to implement

  • Provides integration with background jobs

  • Allows testing notifications from the command line


  • Documentation does not cover some use cases (i.e. Safari push notifications)


Step 1

Add Houston gem to your Gemfile:

gem 'houston'

Run bundle install. Houston comes with Apple Push Notification binary which provides testing notifications from the command line.

Step 2

If you don’t have it yet - create a Device model which will store users’ unique device id (uuid) in the database.


Step 3

Add a migration for the Device model. Column `uuid` and references to user table are required in our case, the rest depends on your specifications.


Run the migration.


Step 4

You’ll also need to prepare an action in which the user’s device will be created and saved to the database. You could do it when user registers or the mobile app calls to some API endpoint. 
To create the device object, we’ll use SecureRandom.uuid method:

Device.create(user: user_id, uuid: SecureRandom.uuid)

Step 5

If you’re using environment variables, Houston will read them automatically, so you can add APN_CERTIFICATE and APN_CERTIFICATE_PASSPHRASE to your config file. 
See more configuration options in this section:

If you’re using AppConfig to store variables, you can define them on initializing APN connection. Here’s an example snippet:


Notice the environment option:

The same way you can initialize production:

Step 6

It’s time to create the Notification that will alert a “Hello, World!” message to our user.

notification = token)
notification.alert = "Hello, World!"

There are several options from which you can choose when creating a custom notification:

notification.alert = "Hello, World!
   set alert message
notification.badge = 57
  change a badge count
notification.sound = "sosumi.aiff"
  custom notification sound
notification.category = "INVITE_CATEGORY"
  add a category identifier
notification.content_available = true
  indicate available Newsstand content
notification.custom_data = {foo: "bar"}
  pass custom data

Silent notifications:

If you want to alert user without sound effect, you have to set sound to an empty string: => '', :content_available => true)

For more options, head over to Houston Github page.


Step 7

Everything looks good, so now, using APN method defined in step 4, we can invoke the action that will send a push notification to your device:


Important note: if you want to test receiving the notification on your device - make sure your your app is in background or closed, otherwise the message won’t show up on your screen.


You can also test if the notifications are working from the included command line apn method. 

Use APN push method with your device uuid as and argument and define

-c path_to_your_certificate and -m “your message”

$ apn push "<uuid>" -c /path/to/apple_push_notification.pem -m "Hello from the command line! " 

Step 8 (production)

Apple encourages using a queued background job over synchronous connection to their servers. That is why, Houston allows us to establish a persistent connection to their servers.

You could create your background job this way:


Notice the APPLE_PRODUCTION_GATEWAY_URI used to establish a connection. 
Again - you can also connect to the development gateway: 



Thanks for reaching to the end! I hope my guide will be helpful in your development efforts. To sum up, during this tutorial you have learned:

  • How to prepare your application for managing user unique devices

  • How to setup Houston for sending push notifications to Apple devices

  • How to schedule sending notifications in a background job

Follow Netguru
Join our Newsletter

Read also
Need a successful project?
Estimate project or contact us