Skip to main content

tauri_plugin_background_service/
notifier.rs

1//! Thin wrapper around [`tauri_plugin_notification`] for fire-and-forget
2//! local notifications.
3//!
4//! Errors are logged but never propagated — callers should not need to
5//! handle notification failures.
6
7use tauri::{AppHandle, Runtime};
8use tauri_plugin_notification::NotificationExt;
9
10/// Thin wrapper over `tauri-plugin-notification`.
11///
12/// Fire-and-forget: errors are logged via `log::warn!` and never propagated.
13#[derive(Clone)]
14pub struct Notifier<R: Runtime> {
15    pub(crate) app: AppHandle<R>,
16}
17
18impl<R: Runtime> Notifier<R> {
19    /// Show a local notification with the given title and body.
20    ///
21    /// Errors are logged but not returned — callers should not need to
22    /// handle notification failures.
23    pub fn show(&self, title: &str, body: &str) {
24        if let Err(e) = self
25            .app
26            .notification()
27            .builder()
28            .title(title)
29            .body(body)
30            .show()
31        {
32            log::warn!("background-service: notification failed: {e}");
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    /// Compile-time test: Notifier can be constructed and cloned from an AppHandle.
42    /// (Does not call show() because that requires a running Tauri app.)
43    #[allow(dead_code)]
44    fn notifier_clone_compiles<R: Runtime + Clone>(app: AppHandle<R>) {
45        let n = Notifier { app };
46        let _cloned = n.clone();
47    }
48}