Skip to content

APNS iOS and OS X

Nicos Karalis edited this page Nov 26, 2012 · 2 revisions

The same way described here can be used to send push notifications to iOS devices and Mac OS X devices.

Configuration

  1. In Keychain access export your certificate and your private key as a p12.

![Keychain Access](https://raw.github.com/NicosKaralis/pushmeup/master/Keychain Access.jpg)

  1. Run the following command to convert the p12 to a pem file

     $ openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
    
  2. After you have created your pem file. Set the host, port and certificate file location on the APNS class. You just need to set this once:

     APNS.host = 'gateway.push.apple.com' 
     # gateway.sandbox.push.apple.com is default
     
     APNS.port = 2195 
     # this is also the default. Shouldn't ever have to set this, but just in case Apple goes crazy, you can.
    
     APNS.pem  = '/path/to/pem/file'
     # this is the file you just created
    
     APNS.pass = ''
     # Just in case your pem need a password
    

Usage

Sending a single notification:

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

Sending multiple notifications

    device_token = '123abc456def'
    n1 = APNS::Notification.new(device_token, 'Hello iPhone!' )
    n2 = APNS::Notification.new(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
    APNS.send_notifications([n1, n2])

Sending more information along

    APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default', 
                                        :other => {:sent => 'with apns gem', :custom_param => "value"})

this will result in a payload like this:

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

Getting your iOS 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);
}

Feedback

When you need to know if the pushes are being sent or if there is any invalid token there is a feedback method.

This method returns an array of objects containing the timestamp and token device that no longer has the app installed.

APNS.feedback
=> [{:timestamp=>2012-11-25 22:34:14 -0200, :token=>"560...59e"}] 

Caution: Every time this method is called, the APNs clean data. So when you run the method twice to get the data first run and the second not.

APNS.feedback
=> [{:timestamp=>2012-11-25 22:34:14 -0200, :token=>"560...59e"}]
APNS.feedback
=> []

If you still cant read the feedback try following this steps:

  1. Install your app and register to push notifications
  2. Send a push to the device
  3. After that remove the app from the device
  4. Send another push to that device (this time it won't arrive)
  5. Now the first call of APNS.feedback will read this token

If you still have problems with feedback please submit a issue