Crate a2[][src]

Expand description


A2 is an asynchronous client to Apple push notification service. It provides a type-safe way to generate correct requests, mapping responses into corresponding types. The client supports both, certificate and token based authentication.

To create a connection it is required to have either a PKCS12 database file including a valid certificate and private key with a password for unlocking it, or a private key in PKCS8 PEM format with the corresponding team and key ids. All of these should be available from Xcode or your Apple developer account.


Building the notification payload should be done with the corresponding builders:

The payload generated by the builder can hold a custom data section, defined by a selected root key. Any data using #[derive(Serialize)] from Serde works, allowing usage of type-safe structs or dynamic hashmaps to generate the custom data.


The asynchronous client, works either with certificate or token authentication.

Example sending a plain notification using token authentication:

let mut builder = PlainNotificationBuilder::new("Hi there");

let payload ="device-token-from-the-user", Default::default());
let mut file = File::open("/path/to/private_key.p8")?;

let client = Client::token(
    &mut file,

let response = client.send(payload).await?;
println!("Sent: {:?}", response);

Example sending a silent notification with custom data using certificate authentication:

#[macro_use] extern crate serde_derive;

use a2::{
    Client, Endpoint, SilentNotificationBuilder, NotificationBuilder, NotificationOptions,
use std::fs::File;

#[derive(Serialize, Debug)]
struct CorporateData {
    tracking_code: &'static str,
    is_paying_user: bool,

async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let tracking_data = CorporateData {
        tracking_code: "999-212-UF-NSA",
        is_paying_user: false,

    let mut payload = SilentNotificationBuilder::new()
        NotificationOptions {
            apns_priority: Some(Priority::Normal),
    payload.add_custom_data("apns_gmbh", &tracking_data)?;

    let mut file = File::open("/path/to/cert_db.p12")?;

    let client = Client::certificate(
        &mut file,
        "Correct Horse Battery Stable",

    let response = client.send(payload).await?;
    println!("Sent: {:?}", response);



pub use crate::request::notification::NotificationBuilder;
pub use crate::response::ErrorBody;
pub use crate::response::ErrorReason;
pub use crate::response::Response;
pub use crate::client::Client;
pub use crate::client::Endpoint;
pub use crate::error::Error;


The client module for sending requests and parsing responses

Error and result module

The request payload module

The APNs response types


A builder to create a localized APNs payload.

Headers to specify options to the notification.

A builder to create a simple APNs notification payload.

A builder to create an APNs silent notification payload which can be used to send custom data to the user’s phone if the user hasn’t been running the app for a while. The custom data needs to be implementing Serialize from Serde.

A builder to create a simple APNs notification payload.


The importance how fast to bring the notification for the user..