NotificationManager

Trait NotificationManager 

Source
pub trait NotificationManager
where Self: Send + Sync + Debug,
{ // Required methods fn get_notification_permission_state<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn first_time_ask_for_notification_permission<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn register( &self, handler_callback: Box<dyn Fn(NotificationResponse) + Send + Sync + 'static>, categories: Vec<NotificationCategory>, ) -> Result<(), Error>; fn remove_all_delivered_notifications(&self) -> Result<(), Error>; fn remove_delivered_notifications( &self, ids: Vec<&str>, ) -> Result<(), Error>; fn get_active_notifications<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<Box<dyn NotificationHandle>>, Error>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn send_notification<'life0, 'async_trait>( &'life0 self, builder: NotificationBuilder, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn NotificationHandle>, Error>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; }
Expand description

Manager for active notifications.

It is needed to display notifications and to manage active notifications.

§Send a notification with a button

let manager = get_notification_manager("com.example.my.app".to_string(), None);
let categories = vec![
    NotificationCategory {
        identifier: "my.app.question".to_string(),
        actions: vec![
            NotificationCategoryAction::Action {
                identifier: "my.app.question.yes".to_string(),
                title: "Yes".to_string(),
            },
            NotificationCategoryAction::Action {
                identifier: "my.app.question.no".to_string(),
                title: "No".to_string(),
            },
        ],
    },
];
manager.register(
    Box::new(|response| {
        log::info!("got notification response: {response:?}");
    }),
    categories,
)?;
let notification = user_notify::NotificationBuilder::new()
   .title("Question")
   .body("are you fine?")
   .set_category_id("my.app.question");
let notification_handle = manager.send_notification(notification).await?;

Note that on macOS you need to ask for permission on first start of your app, before you can send notifications:

if let Err(err) = manager
   .first_time_ask_for_notification_permission()
   .await
{
   println!("failed to ask for notification permission: {err:?}");
}

Required Methods§

Source

fn get_notification_permission_state<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Returns whether the app is allowed to send notifications

Needs to be called from main thread.

§Platform specific:
  • MacOS: “Authorized”, “Provisional” and “Ephemeral” return true. “Denied”, “NotDetermined” and unknown return false.
  • Other: no-op on other platforms (always returns true)
Source

fn first_time_ask_for_notification_permission<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Ask for notification permission.

Needs to be called from main thread.

§Platform specific:
  • MacOS: only asks the user on the first time this method is called.
  • Other: no-op on other platforms (always returns true)
Source

fn register( &self, handler_callback: Box<dyn Fn(NotificationResponse) + Send + Sync + 'static>, categories: Vec<NotificationCategory>, ) -> Result<(), Error>

Registers and initializes the notification handler and categories. Set a function to handle user responses (clicking notification, closing it, clicking an action on it)

§Platform specific:
  • MacOS: sets the UNUserNotificationCenterDelegate
Source

fn remove_all_delivered_notifications(&self) -> Result<(), Error>

Removes all of your app’s delivered notifications from Notification Center.

§Platform specific:
Source

fn remove_delivered_notifications(&self, ids: Vec<&str>) -> Result<(), Error>

Removes specific delivered notifications by their id from Notification Center.

§Platform specific:
  • Linux: only works for notifications from current session, because notification handles are tracked in memory
Source

fn get_active_notifications<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<Box<dyn NotificationHandle>>, Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Get all deliverd notifications from UNUserNotificationCenter that are still active.

§Platform specific:
Source

fn send_notification<'life0, 'async_trait>( &'life0 self, builder: NotificationBuilder, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn NotificationHandle>, Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Shows notification and returns Notification handle

Implementors§