/APNS

An Apple Push Notification Service gem

Primary LanguageRubyMIT LicenseMIT

APNS

a gem for the Apple Push Notification Service.

The connection to Apple is done as needed and last until it is either closed by the system or is timed out.
This is the prefered way for communicating with Apple’s push servers.

This is tested to work in Rails 3.

Install

sudo gem install apns

Setup:

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:

  
    APNS.host = 'gateway.push.apple.com' 
    # gateway.sandbox.push.apple.com 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):

Sending a push notification is sending a payload to Apple’s servers.

You may create payloads with APNS::Payload.new(, )
The payload is composed of a device-token and a message all mixed and encoded together.
Payload message can either just be a alert string or a hash that lets you specify the alert, badge, sound and any custom field.

  
    device_token = '123abc456def'

    APNS.send_payloads(APNS::Payload.new(device_token, 'Hello iPhone!'))

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

Example (Multiple notifications):

You can also send multiple payloads at once

  
    device_token = '123abc456def'

    p1 = APNS::Payload.new(device_token, 'Hello iPhone!' )

    n2 = APNS::Payload.new(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
    
    APNS.send_payloads([p1, p2])
  

Send other info along with aps

You can send other application specific information as well.

  
    APNS.send_payload(APNS::Payload.new(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default', :sent => 'with apns gem'))
  

This will add the :sent key to the same level as the “aps” key:

  
    {"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.

ApplicationAppDelegate.m


- (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); }

Feedback:

You should check the feedback queue of your application on Apple’s servers to avoid sending notifications for obsolete devices

	
		APNS.feedback.each do |time, token|
			... do stuff with token
		end