A simple gem for sending push notifications directly through Apple's Push Notification Service.
Best uses are in testing, or in production for small services (internal apps etc), it is not liable to scale well.
This gem requires Ruby >=1.9.3 & Daemons.
-
You must first obtain an APNS certificate and then create a .pem file: developer.apple.com
-
You must have a vaild token to push to an iOS device.
This gem is not distributed built. Either clone & build, or use bundler:
gem 'simple_apns', :git => 'https://github.com/julesjans/simple_apns.git'
Once the gem is installed:
require 'simple_apns'
SimpleAPNS::Settings.config do |config|
# Necessary configuration:
config.cert = "/My/Certificates/certificate.pem"
config.pid = "/tmp"
# Optional configuration:
config.mode = :production
config.port = 20000
config.params = [:id]
end
You need to configure:
-
Where the .pem certificate is.
-
Where to locate the .pid file for the server.
Optionally you can configure:
-
The mode, :sandbox (default) or :production
-
The port that the local APNS Server will use, default is: 20000
-
Additional params: the standard message includes a single text string, here you can include other params, e.g. an identifier.
token = "a1a1a1a1a..."
text = "Hello World!"
SimpleAPNS::send_notification(token, text)
To set the badge number:
SimpleAPNS.send_notification(token, text, {badge: 1})
To set a custom sound:
SimpleAPNS.send_notification(token, text, {badge: 0, sound: 'mysound.wav'})
Sending a notification will start the APNS server (if it is not already started). It will remain running, waiting for more notifications to send, until it is stopped:
SimpleAPNS.stop_server
The server can be manually started, or restarted, without sending a message:
SimpleAPNS.start_server
SimpleAPNS.restart_server
Addtionally configured parameters are included in a hash:
SimpleAPNS.send_notification(token, text, {id: 'my_identifier'})
- The notification payload is limited to 256 bytes.
- Test APNS Service feedback:
SimpleAPNS.check_feedback
- Implement testing library.