一个支持即时反馈的 Ruby APNs HTTP/2 插件。
Apnotic is a gem for sending Apple Push Notifications using the HTTP-2 specifics.
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.
Just install the gem:
$ gem install apnoticOr add it to your Gemfile:
gem 'apnotic'Sync pushes are blocking calls that will wait for an APNs response before proceeding.
…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.
…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 is supported. There are several advantages with token-based auth:
First, you will need a token signing key from your Apple developer account.
Then configure your connection for :token authentication:
require 'apnotic'
connection = Apnotic::Connection.new(
auth_method: :token,
cert_path: "key.p8",
key_id: "p8_key_id",
team_id: "apple_team_id"
)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:
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.
Apnotic::ConnectionTo create a new persistent connection:
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:
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.
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.
connection.on(:error) { |exception| puts "Exception has been raised: #{exception}" }If the
:errorcallback 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.
push(notification, timeout: 30) → Apnotic::Response or nil
Sends a notification. Returns nil in case a timeout occurs.
prepare_push(notification) → Apnotic::Push
Prepares an async push.
push = client.prepare_push(notification)push_async(push)
Sends the push asynchronously.
Apnotic::ConnectionPoolFor 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) do |connection|
connection.on(:error) { |exception| puts "Exception has been raised: #{exception}" }
endFor example:
APNOTIC_POOL = Apnotic::ConnectionPool.new({
cert_path: "apns_certificate.pem"
}, size: 5) do |connection|
connection.on(:error) { |exception| puts "Exception has been raised: #{exception}" }
endIt 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) do |connection|
connection.on(:error) { |exception| puts "Exception has been raised: #{exception}" }
endSince
1.4.0.you are required to pass in a block when defining anApnotic::ConnectionPool. This is to enforce a proper implementation of the library. You can read more here.
Apnotic::NotificationTo create a notification for a specific device token:
notification = Apnotic::Notification.new(token)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:
notification = Apnotic::Notification.new(token)
notification.alert = "Notification from Apnotic!"
notification.badge = 2
notification.sound = "bells.wav"
notification.priority = 5For 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::ResponseThe response to a call to connection.push.
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,或尚未同步最近议题。