Apple Push Notifications Toolkit for Ruby 4

Posted by Ben Poweski Thu, 17 Sep 2009 14:56:00 GMT

Lately I’ve been working on an iPhone application that utilizes Push Notifications extensively. While Apple provides great documentation there is still a fair amount of work left up to the developer to send push notification in ordinance to their usage policy.

Since I’m using Rails for the backend I needed a way to maintain a single open SSL connection to prevent setup and tear down for every notification sent. To accomplish this I created an Event Machine based server deamon that that proxies all requests send by Rails application over a local, non SSL socket.

The proxy queues and sends each notification to the Apple notification servers. Additionally I’ve created a command line script to send notifications for testing purposes. My project Apn Server is hosted on Github.

You can add it to your Rails application via

config.gem "bpoweski-apnserver", :lib => 'apnserver', 
  :source => "http://gems.github.com"

You can use either a direct connection to Apple or one through the apnserverd proxy included in the gem. To configure directly to Apple’s server (and also setting up and tearing down a new connection each time). Add the following to your environment.rb.

  ApnServer::Config.pem = '/path/to/pem'
  ApnServer::Config.host = 'gateway.push.apple.com'
  ApnServer::Config.port = 2195

To use the non SSL apnserverd proxy simply drop the PEM configuration option

  ApnServer::Config.host = 'localhost'
  ApnServer::Config.port = 22195

To send a notification from Ruby

  notification = ApnServer::Notification.new
  notification.device_token = Base64.decode64(apns_token)
  notification.alert = message
  notification.badge = 1
  notification.sound = 'default'
  notification.push

You can also send notifications via the line with

  $ apnsend --server gateway.push.apple.com --port 2195 \
     --pem key.pem \
     --b64-token j92f12jh8lqcAwcOVeSIrsBxibaJ0xyCi8/AkmzNlk8= \
     --sound default \
     --alert Hello
Comments

Leave a response

  1. proxy sites 2 months later:

    Going to play with it, right away. Will post updates. Cool bit.

  2. Gal Bar-or 4 months later:

    Trying to get this working on snow leopard, no luck whatsoever. Not sure what to do next… would GLADLY pay for help/code fix if that’s what’s necessary. No more hair to pull out…

  3. Gal Bar-or 4 months later:

    /.gem/ruby/1.8/gems/eventmachine-0.12.10/lib/rubyeventmachine.bundle: [BUG] Segmentation fault ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.2.0]

    Abort

  4. PeterP 5 months later:

    Looks like some good work. Working with Apple iPhone Apps is a whole new challenge - and one I’m not brave enough to undertake yet, but I’m trying to get together some of the building blocks. I’ll add this to the list.

Comments