Skip to main content

Notification

Struct Notification 

Source
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

Source

pub fn new() -> Self

Create a new Notification.

Source

pub fn title(self, title: impl ToString) -> Self

Set the title.

Source

pub fn message(self, message: impl ToString) -> Self

Set the body text.

Source

pub fn subtitle(self, subtitle: impl ToString) -> Self

Set a subtitle.

Source

pub fn maybe_subtitle(self, subtitle: Option<impl ToString>) -> Self

Set a subtitle if present.

Source

pub fn action(self, action: impl Into<Action>) -> Self

Add an action button (auto-registered with macOS).

Source

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.

Source

pub fn default_sound(self) -> Self

Play the default system sound.

Source

pub fn sound<S: Into<String>>(self, sound: S) -> Self

Play a named sound from the app bundle or system library.

Source

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.

Source

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.

Source

pub fn schedule_in(self, delay: Duration) -> Self

Schedule the notification to fire after the given delay from now.

Source

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".

Source

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.

Source

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

Set the app icon badge number. Use 0 to clear the badge.

Source

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.

Source

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

Source

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].

Source

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.

Trait Implementations§

Source§

impl Debug for Notification

Source§

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

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

impl Default for Notification

Source§

fn default() -> Notification

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

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<T> AutoreleaseSafe for T
where T: ?Sized,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.