Easy to use library for Apple's Push Notification Service with Rails 3.
- Works with Rails 3 and Ruby 1.9.
- Uses a daemon process to keep open a persistent connection to the Push Notification Service, as recommended by Apple.
- Uses the enhanced binary format (Figure 5-2) so that delivery errors can be reported.
- Airbrake (Hoptoad) integration.
- Support for dictionary
alert
properties (Table 3-2).
Add rapns to your Gemfile:
gem 'rapns'
Generate the migration, rapns.yml and migrate:
rails g rapns
rake db:migrate
-
Open up Keychain Access and select the
Certificates
category in the sidebar. -
Expand the disclosure arrow next to the iOS Push Services certificate you want to export.
-
Select both the certificate and private key.
-
Right click and select
Export 2 items...
. -
Save the file as
cert.p12
, make sure the File Format isPersonal Information Exchange (p12)
. -
If you decide to set a password for your exported certificate, please read the Configuration section below.
-
Convert the certificate to a .pem, where
<environment>
should bedevelopment
orproduction
, depending on the certificate you exported.openssl pkcs12 -nodes -clcerts -in cert.p12 -out <environment>.pem
-
Move the .pem file into your Rails application under
config/rapns
.
Environment configuration lives in config/rapns/rapns.yml
. For common setups you probably wont need to change this file.
If you want to use rapns in environments other than development or production, you will need to create an entry for it. Simply duplicate the configuration for development or production, depending on which iOS Push Certificate you wish to use.
host
the APNs host to connect to, eithergateway.sandbox.push.apple.com
orgateway.sandbox.push.apple.com
.port
the APNs port. Currently2195
for both hosts.certificate
The path to your .pem certificate,config/rapns
is automatically checked if a relative path is given.certificate_password
(default: blank) the password you used when exporting your certificate, if any.airbrake_notify
(default: true) Enables/disables error notifications via Airbrake.poll
(default: 2) Frequency in seconds to check for new notifications to deliver.connections
(default: 3) the number of connections to keep open to the APNs. Consider increasing this if you are sending a very large number of notifications.pid_file
(default: blank) the file that rapns will write its process ID to. Paths are relative to your project's RAILS_ROOT unless an absolute path is given.
cd /path/to/rails/app
bundle exec rapns <Rails environment>
--foreground
will prevent rapns from forking into a daemon.
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.
n = Rapns::Notification.new
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
n.save!
sound
defaults to1.aiff
. You can either set it to a custom .aiff file, ornil
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 theNSDictionary
argument passed to your iOS app in eitherdidFinishLaunchingWithOptions
ordidReceiveRemoteNotification
.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.
Please refer to Apple's documentation (Tables 3-1 and 3-2).
Not yet implemented!
The APN service provides two mechanism for delivery failure notification:
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.
Not implemented yet!
Fork as usual and go crazy!
Thank you to the following wonderful people for contributing to rapns: