/kw_apn

Apple Push Notification Library

Primary LanguageRubyMIT LicenseMIT

About

Push Notification Library with support for the extended format. This Library is designed to send thousands of APNs at once without dropping a lot of messages on errors. Since version 0.4 it supports the use of multiple App IDs with one process.

Getting Started

For Rails start the generator: kw_apn This will copy the configuration example into your project.

If you not working with Rails you need to specify either Rails.root and Rails.env or RACK_ROOT and RACK_ENV for the gem to work.

Manual: create the File <project_root>/config/kw_apn.yml

structure of the config file should look like this:

environment: 
  cert_file: path to ssl certificate file
  push_host: 'gateway.sandbox.push.apple.com' or 'gateway.push.apple.com' for live
  push_port: 2195
  feedback_host: 'feedback.sandbox.push.apple.com' or 'feedback.push.apple.com' for live
  feedback_port: 2196

Creating cert file

Export the key and the certificate as P12 files and run:

openssl pkcs12 -in apn-cert.p12 -out apn-cert.pem -nodes -clcerts
openssl pkcs12 -in apn-key.p12 -out apn-key.pem -nodes -clcerts

Example

users = MyAPNUsers.all
n = []
payload = {:aps => {:alert => "Something very important for everyone to read", :sound => 'annoying_beep'}}

users.each do |u|
  n << KwAPN::Notification.create(u.token, payload, 0)
end

status, ret = KwAPN::Sender.push(n, 'TestSession')

if status == :ok
  ret.each do |token|
    MyAPNUsers.delete_all('token'=>token)
  end
end

Multiple App IDs

Since version 0.4 it is possible to use kw_apn with multiple App IDs.

Configuration Example:

production:
  'com.mycompany.app1':
    cert_file: config/apn_certs/app1.pem
    cert_key: config/apn_certs/app1.pem
  'com.mycompany.app2':
    cert_file: config/apn_certs/app2.pem
    cert_key: config/apn_certs/app2.pem

Usage Example

... same as above...

status, ret = KwAPN::Sender.push(n, 'TestSession', 'com.mycompany.app1')

Copyright

Distributed under the MIT License. Based in part on Apns4r by Leonid Ponomarev (rdoc.info/projects/thegeekbird/Apns4r)