read

Few months ago Apple announced the release of their latest OS, Mavericks.

One of the major features was the ability to send push notifications to Macs similar to iOS. These notifications like iOS Push Notifications can be customized with your website logo, message and action.

image

Setup

To setup push notification, add the Web Push Notification Rails gem to Gemfile and bundle install.

gem 'web-push-notification-rails'
bundle install

Once the gem is installed you need to download the proper certificates and save it in your Rails app. Create a cert folder inside of your Rails app root and store the certificates generated from the steps below here.

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:

  1. Go to iOS Provisioning Portal (you need to login or register)
  2. Click on “Website Push IDs” on the left side menu
  3. Click on “New Website Push ID” and fill in the 2 fields
  4. After you come back to the listing of website push ids, select the created Push Id and click on “Edit” and then “Create Certificate…”
  5. Follow the steps on About Creating a Certificate Signing Request (CSR)
  6. Once you download the Certficate.cer, double-click to install
  7. In the “Keychain Access” tool right-click on Website Push ID: and select both and right click “Export 2 items…
  8. Change "File Format” to “Personal Information Exchange(.p12)” and save (preferably to Rails.root/cert/)
  9. 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

  1. Download http://developer.apple.com/certificationauthority/AppleWWDRCA.cer
  2. Double-click to install
  3. In the “Keychain Access” tool right-click on “Apple Worldwide Developer Relations Certification Authority” and click on Export “Apple….
  4. 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.

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.

Sending Push notification

Currently the gem doesn’t provide functionality to send push notification message so to test sending notification use a gem called Houston. Once installed you can send push notifications by running the following command. Change the device token along with the certificate path and make sure to run in production environment as it only works in Production.

  apn push "64BE8308E51B6B91C0D41A8C856274977A6A45650049435C02F5E9944AB9D7F7" -c cert/apple_push_notification.pem -e production -P "{\"aps\":{\"alert\":{\"title\":\"Title test\",\"body\":\"Body test\"},\"url-args\":[\"someparams\"]}}"

Notes

You will need SSL certificate to make this work and if you want to try it for free you can get a trial SSL certificate for your server through Comodo.

Also you will need to send push notification to production APNS rather than sandbox.

Checkout the Apple WWDC Session Video if you want detailed overview on how the Safari push notifications work. 

If you want to contribute to this Gem, submit a pull request.

Blog Logo

Ankur Patel


Published


If you like my blog you might also enjoy reading my book called Hands-on Full-stack Swift Development

Purchase a paperback copy or eBook from Amazon, Barnes and Noble or other online retailers.

Image

Ankur અંકુર Patel

Full Stack Ruby on Rails, Frontend Web and native iOS App Developer | Author of "Hands-on Full-Stack Development with Swift" amazon.com/author/ankurpatel

Back to Overview