An Apple Push Notification Service gem
Switch branches/tags
Nothing to show
Pull request Compare This branch is 12 commits ahead, 19 commits behind jpoz:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


a gem for the Apple Push Notification Service.


sudo gem install apns


Convert your certificate

In Keychain access export your certificate as a p12. Then run the following command to convert it to a .pem

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

After you have your .pem file. Set what host, port, certificate file location on the APNS class: = ''
# is default

APNS.pem  = '/path/to/pem/file'
# this is the file you just created

APNS.port = 2195
# this is also the default. Shouldn't ever have to set this, but just in case
# Apple goes crazy, you can.

Example (Single notification):

Then to send a push notification you can either just send a string as the alert or give it a hash for the alert, badge and sound.

device_token = '123abc456def'

APNS.send_notification(device_token, 'Hello iPhone!' )
APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')

Example (Multiple notifications):

You can also send multiple notifications using the same connection to Apple:

device_token = '123abc456def'

n1 =, 'Hello iPhone!' )
n2 =, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')

APNS.send_notifications([n1, n2])

Send other info along with aps

You can send other application specific information as well.

  :alert => 'Hello iPhone!',
  :badge => 1,
  :sound => 'default',
  :other => {:sent => 'with apns gem'}

This will add the other hash to the same level as the aps hash:

{"aps":{"alert":"Hello iPhone!","badge":1,"sound":"default"},"sent":"with apns gem"}

Getting your iPhone's device token

After you setup push notification for your application with Apple. You need to ask Apple for you application specific device token.

- (void)applicationDidFinishLaunching:(UIApplication *)application {
	// Register with apple that this app will use push notification
	[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | 
	UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
	// Show the device token obtained from apple to the log
	NSLog(@"deviceToken: %@", deviceToken);

Creating a second client

Sometimes it can be useful to have a second (or third!) client to work with to manage multiple credential sets.

client =
client.send_notification(device_token, :alert => 'hello!')