Apple Safari Web Push Notification Gem for Rails
Overview
Checkout the Apple WWDC Session Video before if you want an overview on how the Safari push notifications work.
Usage
Install
gem 'web-push-notification-rails'
bundle install
P12 certificate
This the first of 2 certificates you need to sign. Apple has a step-by-step for most of it. You can pick up from step 7. Alternatively, you can follow all of my steps:
- Go to iOS Provisioning Portal (you need to login or register)
- Click on "Website Push IDs" on the left side menu
- Click on "New Website Push ID" and fill in the 2 fields
- After you come back to the listing of website push ids, select the created Push Id and click on "Edit" and then "Create Certificate..."
- Follow the steps on About Creating a Certificate Signing Request (CSR)
- Once you download the Certficate.cer, double-click to install
- In the "Keychain Access" tool right-click on Website Push ID: <web.company.id> and select both and right click "Export 2 items...
- Change "File Format" to "Personal Information Exchange(.p12)" and save (preferably to Rails.root/cert/)
- The password you enter during the saving process will go into the initializer ("Run Generator" step)
WWDR Certificate
Second certificate you need to sign the web package
- Double-click to install
- In the "Keychain Access" tool right-click on "Apple Worldwide Developer Relations Certification Authority" and click on Export "Apple....
- Change "File Format" to "Privacy Enhanced Mail (.pem)" and save it (preferably to Rails.root/cert/)
Run generators
Last password parameter is optional. You can just edit the initializer later
rails g web_notification:notification_package website_name website_service_url website_push_id website_allowed_domain website_url_formatted_string wwdr_certificate_path key_path [cert_password]
Example:
rails g web_notification:notification_package mixtapp https://www.mixtapp.co web.com.encoredevlabs.mixtapp https://www.mixtapp.co "https://www.mixtapp.co?%@" cert/AppleWWDRCA.cer cert/website_aps_production.cer.p12 password
If password is blank do not enter the last parameter: cert_password and a blank password will be used.
Customize
If you need to change the password or location of the certificates later you can change it in the web_notification_package_initializer.rb
file.
The package sent to Apple is inside notfication/web_package
folder in Rails.root. Customize the icons inside icon.iconset folder. You can also customize the website.json
which was genereated using the values specified in the generator. Checkout the Apple documentation link to see what values you need to set in the json file.
Apple Callback to Controller
All request from Apple regarding the web package will be sent to apple_web_notifications_controller.rb
which you can customize or you can change the generated routes in the routes.rb file and send the Apple callback methods to your desired controller.
You will need to implement what needs to be done once you get the device token from Apple.
Features to be implemented
Ability to send Push Notification from the rails server as currently I am testing by sending notification using the Houston like this
apn push "64BE8308E51B6B91C0D41A8C856274977A6A45650049435C02F5E9944AB9D7F7" -c cert/apple_push_notification.pem -e production -P "{\"aps\":{\"alert\":{\"title\":\"Title test\",\"body\":\"Body test\"},\"url-args\":[\"someparams\"]}}"
If you have any questions or need help, find me on Twitter: @ankurpatel