Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

apnotic

> 后端框架
Open source

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

503 stars0 likes3 views
WebsiteGitHub

About

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

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:

ruby
gem 'apnotic'

Usage

Standalone

Sync pushes

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

…

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.

…

Mobile Device Management (MDM) notifications

If you are building an iOS MDM solution, you can as well use apnotic to send mdm push notifications with the Apnotic::MdmNotification class. Sending a MDM notification requires a token and a push magic value, which is sent by the iOS device during its MDM enrollment:

…

Token-based authentication

Token-based authentication is supported. There are several advantages with token-based auth:

  • There is no need to renew push certificates annually.
  • A single key can be used for every app in your developer account.

First, you will need a token signing key from your Apple developer account.

Then configure your connection for :token authentication:

ruby
require 'apnotic'
connection = Apnotic::Connection.new(
  auth_method: :token,
  cert_path: "key.p8",
  key_id: "p8_key_id",
  team_id: "apple_team_id"
)

With Sidekiq / Resque / ...

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.

…

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:

ruby
Apnotic::Connection.new(options)
Option Description
:cert_path Required The path to a valid APNS push certificate or any object that responds to :read. Supported formats: .pem, .p12 (:cert auth), or .p8 (:token auth).
:cert_pass Optional The certificate's password.
:auth_method Optional The options are :cert or :token. Defaults to :cert.
:team_id Required for :token auth Team ID from Membership Details.
:key_id Required for :token auth ID from Certificates, Identifiers & Profiles.
:url Optional Defaults to https://api.push.apple.com:443.
:connect_timeout Optional Expressed in seconds, defaults to 30.
:proxy_addr Optional Proxy server. e.g. http://proxy.example.com
:proxy_port Optional Proxy port. e.g. 8080
:proxy_user Optional User name for proxy authentication. e.g. user_name
:proxy_pass Optional Password for proxy authentication. e.g. pass_word

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:

ruby
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:

ruby
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

  • cert_path → string

    Returns the path to the certificate.

  • on(event, &block)

    Allows to set a callback for the connection. The only available event is :error, which allows to set a callback when an error is raised at socket level, hence in the underlying socket thread.

    ruby
    connection.on(:error) { |exception| puts "Exception has been raised: #{exception}" }

    If the :error callback is not set, the underlying socket thread may raise an error in the main thread at unexpected execution times.

  • url → URL

    Returns the URL of the APNS endpoint.

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.

    ruby
    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:

ruby
Apnotic::ConnectionPool.new(connection_options, connection_pool_options) do |connection|
  connection.on(:error) { |exception| puts "Exception has been raised: #{exception}" }
end

For example:

ruby
APNOTIC_POOL = Apnotic::ConnectionPool.new({
  cert_path: "apns_certificate.pem"
}, size: 5) do |connection|
  connection.on(:error) { |exception| puts "Exception has been raised: #{exception}" }
end

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

ruby
Apnotic::ConnectionPool.development(connection_options, connection_pool_options) do |connection|
  connection.on(:error) { |exception| puts "Exception has been raised: #{exception}" }
end

Since 1.4.0. you are required to pass in a block when defining an Apnotic::ConnectionPool. This is to enforce a proper implementation of the library. You can read more here.

Apnotic::Notification

To create a notification for a specific device token:

ruby
notification = Apnotic::Notification.new(token)

Methods

These are all Accessor attributes.

Method Documentation
alert Refer to the official Apple documentation of The Payload Key Reference for details.
badge "
sound "
content_available "
category "
custom_payload "
thread_id "
target_content_id "
interruption_level Refer to Payload Key Reference for details. iOS 15+
relevance_score Refer to Payload Key Reference for details. iOS 15+
stale_date Refer to Payload Key Reference for details. iOS 16+
content_state Refer to Payload Key Reference for details. iOS 16+
timestamp Refer to Payload Key Reference for details. iOS 16+
event Refer to Payload Key Reference for details. iOS 16+
dismissal_date Refer to Payload Key Reference for details. iOS 16+
apns_id Refer to Communicating with APNs for details.
expiration "
priority "
topic "
push_type Refer to Sending Notification Requests to APNs for details, defaults to alert or background (when content-availabe key is set to 1)
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:

ruby
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:

ruby
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

  • body → hash or string

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

  • headers → hash

    Returns a Hash containing the Header

Issues· 16 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyapnsappleclientconnection-pool

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架