pub struct Notification { /* private fields */ }Expand description
A notification with title, body, and optional subtitle / sound.
Construct with Notification::new and chain setter methods.
§Example
use mac_usernotifications::Notification;
let n = Notification::new()
.title("Title")
.message("Body text")
.subtitle("A subtitle")
.sound("Submarine");Implementations§
Source§impl Notification
impl Notification
Sourcepub fn maybe_subtitle(self, subtitle: Option<impl ToString>) -> Self
pub fn maybe_subtitle(self, subtitle: Option<impl ToString>) -> Self
Set a subtitle if present.
Sourcepub fn action(self, action: impl Into<Action>) -> Self
pub fn action(self, action: impl Into<Action>) -> Self
Add an action button (auto-registered with macOS).
Sourcepub fn timeout(self, duration: Duration) -> Self
pub fn timeout(self, duration: Duration) -> Self
Automatically close the notification after duration if the user has not
interacted with it yet.
When the timer fires, the notification banner is removed from the screen
and NotificationHandle::response
resolves with Ok(response) where
NotificationResponse::is_timed_out
returns true.
Without a timeout, “Clear All” in Notification Center causes an indefinite wait.
Sourcepub fn default_sound(self) -> Self
pub fn default_sound(self) -> Self
Play the default system sound.
Sourcepub fn sound<S: Into<String>>(self, sound: S) -> Self
pub fn sound<S: Into<String>>(self, sound: S) -> Self
Play a named sound from the app bundle or system library.
Sourcepub fn maybe_sound<S: Into<String>>(self, sound: Option<S>) -> Self
pub fn maybe_sound<S: Into<String>>(self, sound: Option<S>) -> Self
Play a named sound from the app bundle or system library, if present.
Sourcepub fn image_path(self, path: impl ToString) -> Self
pub fn image_path(self, path: impl ToString) -> Self
Attach an image to the notification by file path.
macOS copies the file at path into its own notification data store.
The framework manages the stored copy; do not rely on the original path
remaining accessible after the notification is posted.
Sourcepub fn schedule_in(self, delay: Duration) -> Self
pub fn schedule_in(self, delay: Duration) -> Self
Schedule the notification to fire after the given delay from now.
Sourcepub fn thread_id(self, id: impl ToString) -> Self
pub fn thread_id(self, id: impl ToString) -> Self
Group this notification with others sharing the same thread identifier.
macOS displays grouped notifications collapsed in Notification Center.
Use a stable per-conversation or per-topic string, e.g. "chat.alice".
Sourcepub fn id(self, id: &str) -> Self
pub fn id(self, id: &str) -> Self
Set an explicit notification identifier.
Posting a new notification with the same identifier replaces any previously displayed notification with that ID. Useful for updating live statuses (e.g. a progress indicator or a changing score).
If not set, a fresh UUID is generated per send.
Sourcepub fn badge(self, count: u32) -> Self
pub fn badge(self, count: u32) -> Self
Set the app icon badge number. Use 0 to clear the badge.
Sourcepub fn interruption_level(self, level: InterruptionLevel) -> Self
pub fn interruption_level(self, level: InterruptionLevel) -> Self
Set the interruption level for this notification (macOS 12+).
Controls whether the notification breaks through Focus modes.
See InterruptionLevel for the available levels.
Sourcepub fn relevance_score(self, score: f64) -> Self
pub fn relevance_score(self, score: f64) -> Self
Set the relevance score for sorting within a notification group.
Value between 0.0 and 1.0. Higher scores appear first when notifications
are grouped by thread identifier.
Source§impl Notification
Sending
impl Notification
Sending
Sourcepub async fn send(self) -> Result<NotificationHandle, Error>
pub async fn send(self) -> Result<NotificationHandle, Error>
Send the notification and return a NotificationHandle once it is delivered.
The future resolves as soon as macOS accepts the notification request.
Call .response().await on the handle to wait for the user’s interaction,
or drop it to ignore the response.
UNUserNotificationCenter always delivers callbacks on the main thread’s run loop.
The main thread must pump NSRunLoop while awaiting the response future.
GUI apps do this automatically; CLI/Tokio apps should use
crate::block_on_main or [crate::run_main_loop_while].
Sourcepub fn send_blocking(self) -> Result<NotificationHandle, Error>
pub fn send_blocking(self) -> Result<NotificationHandle, Error>
Send the notification, blocking until delivered, then return a NotificationHandle.
Waits for macOS to accept the notification request, then returns a handle. Call
crate::block_on_main(handle.response()) to block waiting for the user’s
response, or drop the handle to ignore it.