h1. Apple Push Notification Server Toolkit * http://github.com/bpoweski/apnserver h2. Description apnserver is a server and set of command line programs to send push notifications to the iPhone. Apple recomends to maintain an open connection to the push notification service and refrain from opening up and tearing down SSL connections reapeated. To solve this problem an intermediate network server is introduced that queues are requests to the APN service and sends them via a persistent connection. h2. Remaining Tasks * Implement feedback service mechanism * Implement robust notification sending in reactor periodic scheduler h2. Issues Fixed * second attempt at retry logic, SSL Errors close down sockets now * apnsend --badge option correctly sends integer number rather than string of number for aps json payload * connections are properly closed in Notification#push method now * better compatibility with Rails h2. APN Server Daemon
  
Usage: apnserverd [options] --pem /path/to/pem
  --bind-address bind address (defaults to 0.0.0.0)
    bind address of the server daemon
  
  --proxy-port port 
    the port that the daemon will listen on (defaults to 22195)
  
  --server server
    APN Server (defaults to gateway.push.apple.com)
  
  --port port of the APN Server
    APN server port (defaults to 2195)
  
  --pem pem file path
    The PEM encoded private key and certificate.
    To export a PEM ecoded file execute 
    # openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
 
  --help
    usage message  
  
  --daemon
    Runs process as daemon, not available on Windows
  
h2. APN Server Client With the APN server client script you can send push notifications directly to Apple's APN server over an SSL connection or to the above daemon using a plain socket. To send a notification to Apple's APN server using SSL the *--pem* option must be used.
  
Usage: apnsend [switches] (--b64-token | --hex-token) 
  --server                  the apn server defaults to a locally running apnserverd
  --port <2195>                        the port of the apn server
  --pem                          the path to the pem file, if a pem is supplied the server 
                                       defaults to gateway.push.apple.com:2195
  --alert                     the message to send"
  --sound                     the sound to play, defaults to 'default'
  --badge                      the badge number
  --custom                a custom json string to be added to the main object
  --b64-token                   a base 64 encoded device token
  --hex-token                   a hex encoded device token
  --help                               this message
  
To send a base64 encoded push notification via the command line execute the following:
  
  $ apnsend --server gateway.push.apple.com --port 2195 --pem key.pem \
     --b64-token j92f12jh8lqcAwcOVeSIrsBxibaJ0xyCi8/AkmzNlk8= --sound default \
     --alert Hello
  
h2. Sending Notifications from Ruby To configure the client to send to the local apnserverd process configure the ApnServer client with the following.
  
  # configured for a using the apnserverd proxy
  ApnServer::Config.host = 'localhost'
  ApnServer::Config.port = 22195
  
To configure the client to send directly to Apple's push notification server, bypassing the apnserverd process configure the following.
  
  ApnServer::Config.pem = '/path/to/pem'
  ApnServer::Config.host = 'gateway.push.apple.com'
  ApnServer::Config.port = 2195
  
Finally within we can send a push notification using the following code
  
  notification = ApnServer::Notification.new
  notification.device_token = Base64.decode64(apns_token) # if base64 encoded
  notification.alert = message
  notification.badge = 1
  notification.sound = 'default'
  notification.push
  
h2. Installation To install apnserver execute the following gem command:

  $ gem install bpoweski-apnserver --source http://gems.github.com

Adding apnserver to your Rails application
  
  config.gem "bpoweski-apnserver", :lib => 'apnserver', :source => "http://gems.github.com"
  
h2. License (The MIT License) Copyright (c) 2009 Ben Poweski Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.