/apnotic

A Ruby APNs HTTP/2 gem able to provide instant feedback.

Primary LanguageRubyMIT LicenseMIT

Build Status Code Climate Gem Version

Apnotic

Apnotic is a gem for sending Apple Push Notifications using the HTTP-2 specifics.

Why "Yet Another APN" gem?

If you have used the previous Apple Push Notification specifications you may have noticed that it was hard to know whether a Push Notification was successful or not. It was a common problem that has been reported multiple times. In addition, you had to run a separate Feedback service to retrieve the list of the device tokens that were no longer valid, and ensure to purge them from your systems.

All of this is solved by using the HTTP-2 APN specifications. Every Push Notification you make returns a response stating if the Push was successful or, if not, which problems were encountered. This includes the case when invalid device tokens are used, hence making it unnecessary to have a separate Feedback service.

Installation

Just install the gem:

$ gem install apnotic

Or add it to your Gemfile:

gem 'apnotic'

Usage

Standalone

Sync pushes

Sync pushes are blocking calls that will wait for an APNs response before proceeding.

require 'apnotic'

# create a persistent connection
connection = Apnotic::Connection.new(cert_path: "apns_certificate.pem", cert_pass: "pass")

# create a notification for a specific device token
token = "6c267f26b173cd9595ae2f6702b1ab560371a60e7c8a9e27419bd0fa4a42e58f"

notification       = Apnotic::Notification.new(token)
notification.alert = "Notification from Apnotic!"

# send (this is a blocking call)
response = connection.push(notification)

# read the response
response.ok?      # => true
response.status   # => '200'
response.headers  # => {":status"=>"200", "apns-id"=>"6f2cd350-bfad-4af0-a8bc-0d501e9e1799"}
response.body     # => ""

# close the connection
connection.close

Async pushes

If you are sending out a considerable amount of push notifications, you may consider using async pushes to send out multiple requests in non-blocking calls. This allows to take full advantage of HTTP/2 streams.

require 'apnotic'

# create a persistent connection
connection = Apnotic::Connection.new(cert_path: "apns_certificate.pem", cert_pass: "pass")

# create a notification for a specific device token
token = "6c267f26b173cd9595ae2f6702b1ab560371a60e7c8a9e27419bd0fa4a42e58f"

notification       = Apnotic::Notification.new(token)
notification.alert = "Notification from Apnotic!"

# prepare push
push = connection.prepare_push(notification)
push.on(:response) do |response|
  # read the response
  response.ok?      # => true
  response.status   # => '200'
  response.headers  # => {":status"=>"200", "apns-id"=>"6f2cd350-bfad-4af0-a8bc-0d501e9e1799"}
  response.body     # => ""
end

# send
connection.push_async(push)

# wait for all requests to be completed
connection.join

# close the connection
connection.close

With Sidekiq / Rescue / ...

In case that errors are encountered, Apnotic will raise the error and repair the underlying connection, but it will not retry the requests that have failed. This is by design, so that the job manager (Sidekiq, Resque,...) can retry the job that failed. For this reason, it is recommended to use a queue engine that will retry unsuccessful pushes.

A practical usage of a Sidekiq / Rescue worker probably has to:

  • Use a pool of persistent connections.
  • Send a push notification.
  • Remove a device with an invalid token.
  • Raise errors when requests timeout, so that the queue engine can retry those.

An example of a Sidekiq worker with such features follows. This presumes a Rails environment, and a model Device.

require 'apnotic'

class MyWorker
  include Sidekiq::Worker

  sidekiq_options queue: :push_notifications

  APNOTIC_POOL = Apnotic::ConnectionPool.new({
    cert_path: Rails.root.join("config", "certs", "apns_certificate.pem"),
    cert_pass: "mypass"
  }, size: 5)

  def perform(token)
    APNOTIC_POOL.with do |connection|
      notification       = Apnotic::Notification.new(token)
      notification.alert = "Hello from Apnotic!"

      response = connection.push(notification)
      raise "Timeout sending a push notification" unless response

      if response.status == '410' ||
        (response.status == '400' && response.body['reason'] == 'BadDeviceToken')
        Device.find_by(token: token).destroy
      end
    end
  end
end

The official APNs Provider API documentation explains how to interpret the responses given by the APNS.

You may also consider using async pushes instead in a Sidekiq / Rescue worker.

Objects

Apnotic::Connection

To create a new persistent connection:

Apnotic::Connection.new(options)
Option Description
:cert_path Required. The path to a valid APNS push certificate in .pem or .p12 format, or any object that responds to :read.
:cert_pass Optional. The certificate's password.
:url Optional. Defaults to https://api.push.apple.com:443.
:connect_timeout Optional. Expressed in seconds, defaults to 30.

Note that since :cert_path can be any object that responds to :read, it is possible to pass in a certificate string directly by wrapping it up in a StringIO object:

Apnotic::Connection.new(cert_path: StringIO.new("pem cert as string"))

It is also possible to create a connection that points to the Apple Development servers by calling instead:

Apnotic::Connection.development(options)

The concepts of PRODUCTION and DEVELOPMENT are different from what they used to be in previous specifications. Anything built directly from XCode and loaded on your phone will have the app generate DEVELOPMENT tokens, while everything else (TestFlight, Apple Store, HockeyApp, ...) will be considered as PRODUCTION environment.

Methods

  • urlURL

Returns the URL of the APNS endpoint.

  • cert_pathstring

Returns the path to the certificate.

Blocking calls
  • push(notification, timeout: 30)Apnotic::Response or nil

Sends a notification. Returns nil in case a timeout occurs.

Non-blocking calls
  • prepare_push(notification)Apnotic::Push

Prepares an async push.

push = client.prepare_push(notification)
  • push_async(push)

Sends the push asynchronously.

Apnotic::ConnectionPool

For your convenience, a wrapper around the Connection Pool gem is here for you. To create a new connection pool:

Apnotic::ConnectionPool.new(connection_options, connection_pool_options)

For example:

APNOTIC_POOL = Apnotic::ConnectionPool.new({
  cert_path: "apns_certificate.pem"
}, size: 5)

It is also possible to create a connection pool that points to the Apple Development servers by calling instead:

Apnotic::ConnectionPool.development(connection_options, connection_pool_options)

Apnotic::Notification

To create a notification for a specific device token:

notification = Apnotic::Notification.new(token)

Methods

These are all Accessor attributes.

Method Documentation
alert Refer to the official Apple documentation of The Notification Payload for details.
badge "
sound "
content_available "
category "
custom_payload "
apns_id Refer to the APNs Provider API for details.
expiration "
priority "
topic "
url_args Values for Safari push notifications.
mutable_content Key for UNNotificationServiceExtension.
apns_collapse_id Key for setting the identification of a notification and allowing for the updating of the content of that notification in a subsequent push. More information avaible in WWDC 2016 - Session 707 Introduction to Notifications. iOS 10+

For example:

notification          = Apnotic::Notification.new(token)
notification.alert    = "Notification from Apnotic!"
notification.badge    = 2
notification.sound    = "bells.wav"
notification.priority = 5

For a Safari push notification:

notification = Apnotic::Notification.new(token)

notification.alert    = {
  title:  "Flight A998 Now Boarding",
  body:   "Boarding has begun for Flight A998.",
  action: "View"
}
notification.url_args = ["boarding", "A998"]

Apnotic::Response

The response to a call to connection.push.

Methods

  • ok?boolean

Returns if the push was successful.

  • headershash

Returns a Hash containing the Headers of the response.

  • statusstring

Returns the status code.

  • bodyhash or string

Returns the body of the response in Hash format if a valid JSON was returned, otherwise just the RAW body.

Apnotic::Push

The push object to be sent in an async call.

Methods

  • on(event, &block)

Allows to set a callback for the request. Available events are:

  • :response: triggered when a response is fully received (called once).

Even if Apnotic is thread-safe, the async callbacks will be executed in a different thread, so ensure that your code in the callbacks is thread-safe.

push.on(:response) { |response| p response.headers }
  • http2_requestNetHttp2::Request

Returns the HTTP/2 request of the push.

Getting Your APNs Certificate

These instructions come from another great gem, apn_on_rails.

Once you have the certificate from Apple for your application, export your key and the apple certificate as p12 files. Here is a quick walkthrough on how to do this:

  1. Click the disclosure arrow next to your certificate in Keychain Access and select the certificate and the key.
  2. Right click and choose Export 2 items….
  3. Choose the p12 format from the drop down and name it cert.p12.

Optionally, you may covert the p12 file to a pem file (this step is optional because Apnotic natively supports p12 files):

$ openssl pkcs12 -in cert.p12 -out apple_push_notification_production.pem -nodes -clcerts

Contributing

So you want to contribute? That's great! Please follow the guidelines below. It will make it easier to get merged in.

Before implementing a new feature, please submit a ticket to discuss what you intend to do. Your feature might already be in the works, or an alternative implementation might have already been discussed.

Do not commit to master in your fork. Provide a clean branch without merge commits. Every pull request should have its own topic branch. In this way, every additional adjustments to the original pull request might be done easily, and squashed with git rebase -i. The updated branch will be visible in the same pull request, so there will be no need to open new pull requests when there are changes to be applied.

Ensure that proper testing is included. To run tests you simply have to be in the project's root directory and run:

$ rake