-
Notifications
You must be signed in to change notification settings - Fork 120
APNS iOS and OS X
The same way described here can be used to send push notifications to iOS devices and Mac OS X devices.
- 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)
-
Run the following command to convert the
p12
to apem
file$ openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
-
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
device_token = '123abc456def'
APNS.send_notification(device_token, 'Hello iPhone!' )
APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
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])
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"}
- (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);
}
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:
- Install your app and register to push notifications
- Send a push to the device
- After that remove the app from the device
- Send another push to that device (this time it won't arrive)
- Now the first call of
APNS.feedback
will read this token
If you still have problems with feedback please submit a issue