Struct a2::request::payload::Payload

source ·
pub struct Payload<'a> {
    pub options: NotificationOptions<'a>,
    pub device_token: &'a str,
    pub aps: APS<'a>,
    pub data: BTreeMap<&'a str, Value>,
}
Expand description

The data and options for a push notification.

Fields§

§options: NotificationOptions<'a>

Send options

§device_token: &'a str

The token for the receiving device

§aps: APS<'a>

The pre-defined notification payload

§data: BTreeMap<&'a str, Value>

Application specific payload

Implementations§

Client-specific custom data to be added in the payload. The root_key defines the JSON key in the root of the request data, and data the object containing custom data. The data should implement Serialize, which allows using of any Rust collection or if needing more strict type definitions, any struct that has #[derive(Serialize)] from Serde.

Using a HashMap:

let mut payload = SilentNotificationBuilder::new()
    .build("token", Default::default());
let mut custom_data = HashMap::new();

custom_data.insert("foo", "bar");
payload.add_custom_data("foo_data", &custom_data).unwrap();

assert_eq!(
    "{\"aps\":{\"content-available\":1},\"foo_data\":{\"foo\":\"bar\"}}",
    &payload.to_json_string().unwrap()
);

Using a custom struct:

#[derive(Serialize)]
struct CompanyData {
    foo: &'static str,
}

let mut payload = SilentNotificationBuilder::new().build("token", Default::default());
let mut custom_data = CompanyData { foo: "bar" };

payload.add_custom_data("foo_data", &custom_data).unwrap();

assert_eq!(
    "{\"aps\":{\"content-available\":1},\"foo_data\":{\"foo\":\"bar\"}}",
    &payload.to_json_string().unwrap()
);

Combine the APS payload and the custom data to a final payload JSON. Returns an error if serialization fails.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.