Easy to use library for Apple's Push Notification Service with Rails 3.
- Works with Rails 3 and Ruby 1.9 & 1.8.
- 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.
- Records feedback from The Feedback Service.
- Airbrake (Hoptoad) integration.
- Support for dictionary
alert
properties (Table 3-2). - Reconnects to the APNs if connections are lost.
- Reconnects to your database if the connect is lost.
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.
-
push
this section contains options to configure the delivery of notifications.host
the APNs host to connect to, eithergateway.push.apple.com
orgateway.sandbox.push.apple.com
.port
the APNs port. Currently2195
for both hosts.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.
-
feedback
this section contains options to configure feedback checking.host
the APNs host to connect to, eitherfeedback.push.apple.com
orfeedback.sandbox.push.apple.com
.port
the APNs port. Currently2196
for both hosts.poll
(default: 60) Frequency in seconds to check for new feedback.
-
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. -
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).
The APNs 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.
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.
After updating you should run rails g rapns
to check for any new migrations or configuration changes.
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: