[][src]Crate a2

A2

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.

Payload

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.

Client

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");
builder.set_badge(420);
builder.set_category("cat1");
builder.set_sound("ping.flac");

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

let client = Client::token(
    &mut file,
    "KEY_ID",
    "TEAM_ID",
    Endpoint::Production).unwrap();

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, 
    Priority,
};
use std::fs::File;

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

#[tokio::main]
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()
        .build("device-token-from-the-user",
        NotificationOptions {
            apns_priority: Some(Priority::Normal),
            ..Default::default()
        },
    );
    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",
        Endpoint::Production)?;

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

    Ok(())
}

Re-exports

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

Modules

client

The client module for sending requests and parsing responses

error

Error and result module

request

The request payload module

response

The APNs response types

Structs

CollapseId
LocalizedNotificationBuilder

A builder to create a localized APNs payload.

NotificationOptions

Headers to specify options to the notification.

PlainNotificationBuilder

A builder to create a simple APNs notification payload.

SilentNotificationBuilder

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.

Enums

Priority

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