Apple Push Notifications using Apnotic

Push notifications are used across multiple applications and platforms. The use of push has many benefits, including keeping users engaged with your product.

Here we will focus on the server side of push notifications, specifically for IOS applications. This is simplified by using the Apnotic gem.

The biggest advantage of Apnotic over most of the gems created for setting up APNs is that it gives you a return on every Push you send. This makes it much easier to deal with notifications that have not been sent, to remove invalid device tokens from your database without having to use the Apple Feedback service, which returns tokens that are no longer valid.

Installation

In your Rails application, first of all, you need to add Apnotic to your Gemfile.

gem ‘apnotic’

After that use the Bundle command to add Apnotic to your application.

Connection with APNs

Apple recommends some good practices related to APNs connection, one of them is that if your application sends a lot of push notifications, you should open only one connection to send them.

Keep your connections with APNs open across multiple notifications; do not repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack. You should leave a connection open unless you know it will be idle for an extended period of time — for example, if you only send notifications to your users once a day, it is acceptable practice to use a new connection each day.

Apnotic makes connections to APNs very easy to execute. Assuming that you have already set up the necessary on Apple service to send push notifications, and you already have your p12 certificate (there are good tutorials on how to do this). The Apnotic supports p12 files, but you may convert this to pem file, running this command on terminal.

openssl pkcs12 -in your_p12_certificate.p12 -out your_pem_file.pem -aes256 -clcerts

Now we ready to open a connection to APNs using the Apnotic.

In my case I had to pass the APNs url, both development and production, which are stored in the secrets file of my application. Before that I was getting error while sending the notifications.

Development server: api.development.push.apple.com:443
Production server: api.push.apple.com:443

Send notifications

After creating a method to open a connection to the APNs, we must send the notifications. We can do this in the same class.

The send_many method receives a hash of notifications as a parameter. To create a notification with Apnotic, again, is very simple. For this you need to have the device tokens returned by your iOS application.

notification = Apnotic::Notification.new(token)
notification.alert = {
title: 'You notification title',
body: 'You notification message.'
}
notification.badge = 1

Speaking about the send_many method, first of all, a new connection is created. After that, an iteration is done in the notifications collection. For each notification, a new push is prepared. To send it, the push_async method is used, as shown in the Apnotic documentation.

On line 15 is how we see the return of each push. In this example this is only displaying the status code of the response, but you can create a method that will clean your database, calling it when the response is related to BadDeviceToken. You could run your push_service through a Resque/Sidekiq worker.

For now, that’s it, I hope this tutorial can help you in some way, thanks for read. If you know a better way to do this, please leave your comment.