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>
impl<'a> DefaultNotificationBuilder<'a>
Sourcepub fn new() -> DefaultNotificationBuilder<'a>
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()
);Sourcepub fn title(self, title: impl Into<Cow<'a, str>>) -> Self
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()
);pub fn set_title(self, title: impl Into<Cow<'a, str>>) -> Self
title instead of the legacy set_* fnSourcepub fn critical(self, critical: bool, volume: Option<f64>) -> Self
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()
);pub fn set_critical(self, critical: bool, volume: Option<f64>) -> Self
critical instead of the legacy set_* fnSourcepub fn subtitle(self, subtitle: impl Into<Cow<'a, str>>) -> Self
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()
);pub fn set_subtitle(self, subtitle: impl Into<Cow<'a, str>>) -> Self
subtitle instead of the legacy set_* fnSourcepub fn body(self, body: impl Into<Cow<'a, str>>) -> Self
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()
);pub fn set_body(self, body: impl Into<Cow<'a, str>>) -> Self
body instead of the legacy set_* fnSourcepub fn badge(self, badge: u32) -> Self
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()
);pub fn set_badge(self, badge: u32) -> Self
badge instead of the legacy set_* fnSourcepub fn sound(self, sound: impl Into<Cow<'a, str>>) -> Self
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()
);pub fn set_sound(self, sound: impl Into<Cow<'a, str>>) -> Self
sound instead of the legacy set_* fnSourcepub fn thread_id(self, thread_id: impl Into<Cow<'a, str>>) -> Self
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()
);Sourcepub fn category(self, category: impl Into<Cow<'a, str>>) -> Self
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()
);pub fn set_category(self, category: impl Into<Cow<'a, str>>) -> Self
category instead of the legacy set_* fnSourcepub fn subtitle_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self
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()
);Sourcepub fn subtitle_loc_args<S>(self, args: &'a [S]) -> Self
pub fn subtitle_loc_args<S>(self, args: &'a [S]) -> Self
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()
);Sourcepub fn title_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self
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()
);pub fn set_title_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self
title_loc_key instead of the legacy set_* fnSourcepub fn title_loc_args<S>(self, args: &'a [S]) -> Self
pub fn title_loc_args<S>(self, args: &'a [S]) -> Self
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()
);pub fn set_title_loc_args<S>(self, key: &'a [S]) -> Self
title_loc_args instead of the legacy set_* fnSourcepub fn action_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self
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()
);pub fn set_action_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self
action_loc_key instead of the legacy set_* fnSourcepub fn loc_key(self, key: impl Into<Cow<'a, str>>) -> Self
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()
);pub fn set_loc_key(self, key: impl Into<Cow<'a, str>>) -> Self
loc_key instead of the legacy set_* fnSourcepub fn loc_args<S>(self, args: &'a [S]) -> Self
pub fn loc_args<S>(self, args: &'a [S]) -> Self
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()
);pub fn set_loc_args<S>(self, key: &'a [S]) -> Self
loc_args instead of the legacy set_* fnSourcepub fn launch_image(self, image: impl Into<Cow<'a, str>>) -> Self
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()
);pub fn set_launch_image(self, image: impl Into<Cow<'a, str>>) -> Self
launch_image instead of the legacy set_* fnSourcepub fn mutable_content(self) -> Self
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()
);pub fn set_mutable_content(self) -> Self
mutable_content instead of the legacy set_* fnSourcepub fn content_available(self) -> Self
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()
);pub fn set_content_available(self) -> Self
content_available instead of the legacy set_* fnSourcepub fn active_interruption_level(self) -> Self
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()
);Sourcepub fn critical_interruption_level(self) -> Self
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()
);Sourcepub fn passive_interruption_level(self) -> Self
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()
);Sourcepub fn time_sensitive_interruption_level(self) -> Self
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()
);Sourcepub fn interruption_level(self, level: InterruptionLevel) -> Self
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()
);Sourcepub fn timestamp(self, timestamp: u64) -> Self
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()
);Sourcepub fn event(self, event: impl Into<Cow<'a, str>>) -> Self
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()
);Sourcepub fn content_state(self, content_state: &Value) -> Self
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!\"}"));Sourcepub fn attributes_type(self, attributes_type: impl Into<Cow<'a, str>>) -> Self
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()
);Sourcepub fn attributes(self, attributes: &Value) -> Self
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!\"}"));Sourcepub fn input_push_channel(self, channel_id: impl Into<Cow<'a, str>>) -> Self
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()
);Sourcepub fn input_push_token(self) -> Self
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()
);Sourcepub fn dismissal_date(self, dismissal_date: u64) -> Self
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>
impl<'a> Clone for DefaultNotificationBuilder<'a>
Source§fn clone(&self) -> DefaultNotificationBuilder<'a>
fn clone(&self) -> DefaultNotificationBuilder<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more