APNS

a gem for the Apple Push Notification Service.

Install

gem sources -a http://gems.github.com

sudo gem install jpoz-apns

Setup:

Set what host, port, pem file and password on the APNS class:


    APNS.host = 'gateway.push.apple.com' 
    # gateway.sandbox.push.apple.com is default

    APNS.pem  = '/path/to/pem/file'
    APNS.pass = 'secret'
    # openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
    
    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:

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')
  

Send other info along with aps

You can send other application specific information as well.


    APNS.send_notification(device_token, :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.

ApplicationAppDelegate.m


    - (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
    }</p>
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(<code>"deviceToken: %</code>", deviceToken);
}
</code>
<p>