Skip to main content

DefaultNotificationBuilder

Struct DefaultNotificationBuilder 

Source
pub struct DefaultNotificationBuilder<'a> { /* private fields */ }
Expand description

A builder to create an APNs payload.

§Example

let mut builder = DefaultNotificationBuilder::new()
    .title("Hi there")
    .subtitle("From bob")
    .body("What's up?")
    .badge(420)
    .category("cat1")
    .sound("prööt")
    .thread_id("my-thread")
    .critical(false, None)
    .mutable_content()
    .action_loc_key("PLAY")
    .launch_image("foo.jpg")
    .loc_args(&["argh", "narf"])
    .title_loc_key("STOP")
    .title_loc_args(&["herp", "derp"])
    .loc_key("PAUSE")
    .loc_args(&["narf", "derp"]);
let payload = builder.build("device_id", Default::default())
  .to_json_string().unwrap();

Implementations§

Source§

impl<'a> DefaultNotificationBuilder<'a>

Source

pub fn new() -> DefaultNotificationBuilder<'a>

Creates a new builder with the minimum amount of content.

let payload = DefaultNotificationBuilder::new()
    .title("a title")
    .body("a body")
    .build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\",\"body\":\"a body\"},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn title(self, title: impl Into<Cow<'a, str>>) -> Self

Set the title of the notification. Apple Watch displays this string in the short look notification interface. Specify a string that’s quickly understood by the user.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title");
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_title(self, title: impl Into<Cow<'a, str>>) -> Self

👎Deprecated since 0.11.0: Use the idiomatic title instead of the legacy set_* fn
Source

pub fn critical(self, critical: bool, volume: Option<f64>) -> Self

Set critical alert value for this notification Volume can only be set when the notification is marked as critcial Note: You’ll need the critical alerts entitlement to use true!

let mut builder = DefaultNotificationBuilder::new()
    .critical(true, None);
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"sound\":{\"critical\":1},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_critical(self, critical: bool, volume: Option<f64>) -> Self

👎Deprecated since 0.11.0: Use the idiomatic critical instead of the legacy set_* fn
Source

pub fn subtitle(self, subtitle: impl Into<Cow<'a, str>>) -> Self

Used to set the subtitle which should provide additional information that explains the purpose of the notification.

let mut builder = DefaultNotificationBuilder::new()
    .subtitle("a subtitle");
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"subtitle\":\"a subtitle\"},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_subtitle(self, subtitle: impl Into<Cow<'a, str>>) -> Self

👎Deprecated since 0.11.0: Use the idiomatic subtitle instead of the legacy set_* fn
Source

pub fn body(self, body: impl Into<Cow<'a, str>>) -> Self

Sets the content of the alert message.

let mut builder = DefaultNotificationBuilder::new()
    .body("a body");
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"body\":\"a body\"},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_body(self, body: impl Into<Cow<'a, str>>) -> Self

👎Deprecated since 0.11.0: Use the idiomatic body instead of the legacy set_* fn
Source

pub fn badge(self, badge: u32) -> Self

A number to show on a badge on top of the app icon.

let mut builder = DefaultNotificationBuilder::new()
    .badge(4);
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"badge\":4,\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_badge(self, badge: u32) -> Self

👎Deprecated since 0.11.0: Use the idiomatic badge instead of the legacy set_* fn
Source

pub fn sound(self, sound: impl Into<Cow<'a, str>>) -> Self

File name of the custom sound to play when receiving the notification.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .sound("ping");
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"sound\":\"ping\",\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_sound(self, sound: impl Into<Cow<'a, str>>) -> Self

👎Deprecated since 0.11.0: Use the idiomatic sound instead of the legacy set_* fn
Source

pub fn thread_id(self, thread_id: impl Into<Cow<'a, str>>) -> Self

An application-specific name that allows notifications to be grouped together.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .thread_id("my-thread");
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"thread-id\":\"my-thread\",\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn category(self, category: impl Into<Cow<'a, str>>) -> Self

When a notification includes the category key, the system displays the actions for that category as buttons in the banner or alert interface.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .category("cat1");
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"category\":\"cat1\",\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_category(self, category: impl Into<Cow<'a, str>>) -> Self

👎Deprecated since 0.11.0: Use the idiomatic category instead of the legacy set_* fn
Source

pub fn subtitle_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self

The subtitle localization key for the notification title.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .subtitle_loc_key("yolo");
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\",\"subtitle-loc-key\":\"yolo\"},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn subtitle_loc_args<S>(self, args: &'a [S]) -> Self
where S: Into<Cow<'a, str>> + AsRef<str>,

Arguments for the title localization.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .subtitle_loc_args(&["fooz", "barz"]);
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\",\"subtitle-loc-args\":[\"fooz\",\"barz\"]},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn title_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self

The localization key for the notification title.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .title_loc_key("play");
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\",\"title-loc-key\":\"play\"},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_title_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self

👎Deprecated since 0.11.0: Use the idiomatic title_loc_key instead of the legacy set_* fn
Source

pub fn title_loc_args<S>(self, args: &'a [S]) -> Self
where S: Into<Cow<'a, str>> + AsRef<str>,

Arguments for the title localization.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .title_loc_args(&["foo", "bar"]);
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\",\"title-loc-args\":[\"foo\",\"bar\"]},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_title_loc_args<S>(self, key: &'a [S]) -> Self
where S: Into<Cow<'a, str>> + AsRef<str>,

👎Deprecated since 0.11.0: Use the idiomatic title_loc_args instead of the legacy set_* fn
Source

pub fn action_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self

The localization key for the action.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .action_loc_key("stop");
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\",\"action-loc-key\":\"stop\"},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_action_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self

👎Deprecated since 0.11.0: Use the idiomatic action_loc_key instead of the legacy set_* fn
Source

pub fn loc_key(self, key: impl Into<Cow<'a, str>>) -> Self

The localization key for the push message body.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .loc_key("lol");
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\",\"loc-key\":\"lol\"},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self

👎Deprecated since 0.11.0: Use the idiomatic loc_key instead of the legacy set_* fn
Source

pub fn loc_args<S>(self, args: &'a [S]) -> Self
where S: Into<Cow<'a, str>> + AsRef<str>,

Arguments for the content localization.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .loc_args(&["omg", "foo"]);
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\",\"loc-args\":[\"omg\",\"foo\"]},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_loc_args<S>(self, key: &'a [S]) -> Self
where S: Into<Cow<'a, str>> + AsRef<str>,

👎Deprecated since 0.11.0: Use the idiomatic loc_args instead of the legacy set_* fn
Source

pub fn launch_image(self, image: impl Into<Cow<'a, str>>) -> Self

Image to display in the rich notification.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .launch_image("cat.png");
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\",\"launch-image\":\"cat.png\"},\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_launch_image(self, image: impl Into<Cow<'a, str>>) -> Self

👎Deprecated since 0.11.0: Use the idiomatic launch_image instead of the legacy set_* fn
Source

pub fn mutable_content(self) -> Self

Allow client to modify push content before displaying.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .mutable_content();
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"mutable-content\":1}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_mutable_content(self) -> Self

👎Deprecated since 0.11.0: Use the idiomatic mutable_content instead of the legacy set_* fn
Source

pub fn content_available(self) -> Self

Used for adding custom data to push notifications

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .content_available();
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"content-available\":1,\"mutable-content\":0}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn set_content_available(self) -> Self

👎Deprecated since 0.11.0: Use the idiomatic content_available instead of the legacy set_* fn
Source

pub fn active_interruption_level(self) -> Self

Set the interruption level to active. The system presents the notification immediately, lights up the screen, and can play a sound.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .active_interruption_level();
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"mutable-content\":0,\"interruption-level\":\"active\"}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn critical_interruption_level(self) -> Self

Set the interruption level to critical. The system presents the notification immediately, lights up the screen, and bypasses the mute switch to play a sound.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .critical_interruption_level();
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"mutable-content\":0,\"interruption-level\":\"critical\"}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn passive_interruption_level(self) -> Self

Set the interruption level to passive. The system adds the notification to the notification list without lighting up the screen or playing a sound.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .passive_interruption_level();
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"mutable-content\":0,\"interruption-level\":\"passive\"}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn time_sensitive_interruption_level(self) -> Self

Set the interruption level to time sensitive. The system presents the notification immediately, lights up the screen, can play a sound, and breaks through system notification controls.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .time_sensitive_interruption_level();
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"mutable-content\":0,\"interruption-level\":\"time-sensitive\"}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn interruption_level(self, level: InterruptionLevel) -> Self

Set the interruption level directly. Controls how the notification is presented to the user.

let mut builder = DefaultNotificationBuilder::new()
    .title("a title")
    .interruption_level(InterruptionLevel::Active);
let payload = builder.build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"mutable-content\":0,\"interruption-level\":\"active\"}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn timestamp(self, timestamp: u64) -> Self

Set the timestamp for a Live Activity update

let payload = DefaultNotificationBuilder::new()
    .timestamp(1234)
    .build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"mutable-content\":0,\"timestamp\":1234}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn event(self, event: impl Into<Cow<'a, str>>) -> Self

Set the event for a Live Activity. Use “start” to begin a Live Activity.

let payload = DefaultNotificationBuilder::new()
    .event("start")
    .build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"mutable-content\":0,\"event\":\"start\"}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn content_state(self, content_state: &Value) -> Self

Set the content state for a Live Activity with dynamic data

let content_state = json!({
    "currentHealthLevel": 100,
    "eventDescription": "Adventure has begun!"
});
let payload = DefaultNotificationBuilder::new()
    .content_state(&content_state)
    .build("token", Default::default());

assert!(payload.to_json_string().unwrap().contains("\"content-state\":{\"currentHealthLevel\":100,\"eventDescription\":\"Adventure has begun!\"}"));
Source

pub fn attributes_type(self, attributes_type: impl Into<Cow<'a, str>>) -> Self

Set the attributes type for a Live Activity

let payload = DefaultNotificationBuilder::new()
    .attributes_type("AdventureAttributes")
    .build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"mutable-content\":0,\"attributes-type\":\"AdventureAttributes\"}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn attributes(self, attributes: &Value) -> Self

Set the attributes for a Live Activity with data defining the Live Activity

let attributes = json!({
    "currentHealthLevel": 100,
    "eventDescription": "Adventure has begun!"
});
let payload = DefaultNotificationBuilder::new()
    .attributes(&attributes)
    .build("token", Default::default());

assert!(payload.to_json_string().unwrap().contains("\"attributes\":{\"currentHealthLevel\":100,\"eventDescription\":\"Adventure has begun!\"}"));
Source

pub fn input_push_channel(self, channel_id: impl Into<Cow<'a, str>>) -> Self

Set the input push channel ID for iOS 18+ channel-based Live Activity updates

let payload = DefaultNotificationBuilder::new()
    .input_push_channel("dHN0LXNyY2gtY2hubA==")
    .build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"mutable-content\":0,\"input-push-channel\":\"dHN0LXNyY2gtY2hubA==\"}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn input_push_token(self) -> Self

Enable input push token request for iOS 18+ token-based Live Activity updates

let payload = DefaultNotificationBuilder::new()
    .input_push_token()
    .build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"mutable-content\":0,\"input-push-token\":1}}",
    &payload.to_json_string().unwrap()
);
Source

pub fn dismissal_date(self, dismissal_date: u64) -> Self

Set the dismissal date for when the system should automatically remove the notification. The timestamp should be in Unix epoch time (seconds since 1970-01-01 00:00:00 UTC).

let payload = DefaultNotificationBuilder::new()
    .title("a title")
    .dismissal_date(1672531200) // January 1, 2023 00:00:00 UTC
    .build("token", Default::default());

assert_eq!(
    "{\"aps\":{\"alert\":{\"title\":\"a title\"},\"mutable-content\":0,\"dismissal-date\":1672531200}}",
    &payload.to_json_string().unwrap()
);

Trait Implementations§

Source§

impl<'a> Clone for DefaultNotificationBuilder<'a>

Source§

fn clone(&self) -> DefaultNotificationBuilder<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for DefaultNotificationBuilder<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for DefaultNotificationBuilder<'a>

Source§

fn default() -> DefaultNotificationBuilder<'a>

Returns the “default value” for a type. Read more
Source§

impl<'a> NotificationBuilder<'a> for DefaultNotificationBuilder<'a>

Source§

fn build( self, device_token: impl Into<Cow<'a, str>>, options: NotificationOptions<'a>, ) -> Payload<'a>

Generates the request payload to be send with the Client.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more