How to Implement Apple Push Notifications in Rails
I assume you already have these:
-
Apple certificatefor 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.
Pros:
-
easy to implement,
-
provides integration with background jobs,
-
allows testing notifications from the command line.
Cons:
-
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 enables 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.
[code]
Step 3
Add a migration for the Device model. The `uuid` column and references to the user table are required in our case, the rest depends on your specifications.
[code]
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 the user registers or when 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_CERTIFICATEand APN_CERTIFICATE_PASSPHRASEto your config file.
See more configuration options in this section: https://github.com/nomad/houston#configuration.
If you’re using AppConfig to store variables, you can define them on initialising APN connection. Here’s an example of a snippet:
[code]
Notice the environment option:
Houston::Client.development
Houston::Client.production
Step 6
It’s time to create the Notification that will alert a “Hello, World!” message to our user.
notification = Houston::Notification.new(device: token)
notification.alert = "Hello, World!"
There are several options from which you can choose when creating a custom notification:
notification.alert = "Hello, World! |
set an 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 the user without a sound effect, you have to set sound to an empty string:
Houston::Notification.new(:sound => '', :content_available => true)
For more options, head over to Houston Githubpage.
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:
apn.push(notification)
Important note: if you want to test receiving the notification on your device - make sure your app is in the 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:
[code]
Notice the APPLE_PRODUCTION_GATEWAY_URIused to establish a connection.
Again - you can also connect to the development gateway:
Houston::APPLE_DEVELOPMENT_GATEWAY_URI
Summary
Thanks for reaching to the end! I hope my guide will be helpful in your development efforts. To sum up, in this tutorial you have learned:
-
how to prepare your application for managing unique userdevices,
-
how to set up Houston to send push notifications to Apple devices,
-
how to schedule sending notifications in a background job.