pub trait NotificationManager{
// 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§
Sourcefn 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 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 returnfalse. - Other: no-op on other platforms (always returns true)
Sourcefn 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 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)
Sourcefn register(
&self,
handler_callback: Box<dyn Fn(NotificationResponse) + Send + Sync + 'static>,
categories: Vec<NotificationCategory>,
) -> Result<(), Error>
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
Sourcefn remove_all_delivered_notifications(&self) -> Result<(), Error>
fn remove_all_delivered_notifications(&self) -> Result<(), Error>
Removes all of your app’s delivered notifications from Notification Center.
§Platform specific:
- MacOS: UNUserNotificationCenter.removeAllDeliveredNotifications
- Linux: only works for notifications from current session, because notification handles are tracked in memory
Sourcefn remove_delivered_notifications(&self, ids: Vec<&str>) -> Result<(), Error>
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
Sourcefn 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 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:
- MacOS:
- also includes notifications from previous sessions
- UNUserNotificationCenter.getDeliveredNotificationsWithCompletionHandler
- Others: TODO: implemented/emulated by keeping track of all notifications in memory
Sourcefn 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,
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