Apple Push Notification Server Toolkit
- http://github.com/bpoweski/apnserver
Description
apnserver is a server and set of command line programs to send push notifications to the iPhone. Apple recommends to maintain an open connection to the push notification service and refrain from opening up and tearing down SSL connections repeated. 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.
Remaining Tasks
- Implement feedback service mechanism
- Implement robust notification sending in reactor periodic scheduler
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
- removed json gem in favor of ActiveSupport
- Rails 3.x support
- drop the erroneous puts statements in favor a configurable logger
- moved to Rspec
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
--pem-passphrase passphrase
The PEM passphrase to decode key.
--help
usage message
--daemon
Runs process as daemon, not available on Windows
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) <token>
--server <localhost> the apn server defaults to a locally running apnserverd
--port <2195> the port of the apn server
--pem <path> the path to the pem file, if a pem is supplied the server
defaults to gateway.push.apple.com:2195
--pem-passphrase <passphrase> the pem passphrase
--alert <message> the message to send"
--sound <default> the sound to play, defaults to 'default'
--badge <number> the badge number
--custom <json string> a custom json string to be added to the main object
--b64-token <token> a base 64 encoded device token
--hex-token <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
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
ApnServer::Config.logger = Rails.logger
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 =
notification.badge = 1
notification.sound = 'default'
notification.push
Installation
Apnserver is hosted on rubygems
$ gem install apnserver
Adding apnserver to your Rails application
gem 'apnserver', '>= 0.2.0
License
(The MIT License)
Copyright © 2011 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.