From 2d03e2eac2c19ad997d81d23836ab6a219252ffb Mon Sep 17 00:00:00 2001 From: Keerthi <159991565+Keerthi421@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:05:27 +0000 Subject: [PATCH] Add sound support for desktop notifications in Tauri v2 (#2678) * Add sound support for desktop notifications in Tauri v2 * ci --------- Co-authored-by: Lucas Nogueira --- .changes/notification-sound.md | 6 ++++ examples/api/src/views/Notifications.svelte | 18 +++++++--- plugins/notification/README.md | 39 +++++++++++++++++++++ plugins/notification/guest-js/index.ts | 8 ++++- plugins/notification/src/desktop.rs | 16 +++++++++ plugins/notification/src/lib.rs | 2 +- 6 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 .changes/notification-sound.md diff --git a/.changes/notification-sound.md b/.changes/notification-sound.md new file mode 100644 index 00000000..be864bea --- /dev/null +++ b/.changes/notification-sound.md @@ -0,0 +1,6 @@ +--- +"notification": patch +"notification-js": patch +--- + +Added sound support for desktop notifications which was previously only available on mobile platforms. diff --git a/examples/api/src/views/Notifications.svelte b/examples/api/src/views/Notifications.svelte index e01d57fc..11273fdc 100644 --- a/examples/api/src/views/Notifications.svelte +++ b/examples/api/src/views/Notifications.svelte @@ -1,16 +1,21 @@ - diff --git a/plugins/notification/README.md b/plugins/notification/README.md index b42d6f2f..e17efe02 100644 --- a/plugins/notification/README.md +++ b/plugins/notification/README.md @@ -95,6 +95,45 @@ export async function enqueueNotification(title, body) { } ``` +### Notification with Sound + +You can add sound to your notifications on all platforms (desktop and mobile): + +```javascript +import { sendNotification } from '@tauri-apps/plugin-notification' +import { platform } from '@tauri-apps/api/os' + +// Basic notification with sound +sendNotification({ + title: 'New Message', + body: 'You have a new message', + sound: 'notification.wav' // Path to sound file +}) + +// Platform-specific sounds +async function sendPlatformSpecificNotification() { + const platformName = platform() + + let soundPath + if (platformName === 'darwin') { + // On macOS: use system sounds or sound files in the app bundle + soundPath = 'Ping' // macOS system sound + } else if (platformName === 'linux') { + // On Linux: use XDG theme sounds or file paths + soundPath = 'message-new-instant' // XDG theme sound + } else { + // On Windows: use file paths + soundPath = 'notification.wav' + } + + sendNotification({ + title: 'Platform-specific Notification', + body: 'This notification uses platform-specific sound', + sound: soundPath + }) +} +``` + ## Contributing PRs accepted. Please make sure to read the Contributing Guide before making a pull request. diff --git a/plugins/notification/guest-js/index.ts b/plugins/notification/guest-js/index.ts index 9f81a1e1..685c60c2 100644 --- a/plugins/notification/guest-js/index.ts +++ b/plugins/notification/guest-js/index.ts @@ -71,7 +71,13 @@ interface Options { */ groupSummary?: boolean /** - * The sound resource name. Only available on mobile. + * The sound resource name or file path for the notification. + * + * Platform specific behavior: + * - On macOS: use system sounds (e.g., "Ping", "Blow") or sound files in the app bundle + * - On Linux: use XDG theme sounds (e.g., "message-new-instant") or file paths + * - On Windows: use file paths to sound files (.wav format) + * - On Mobile: use resource names */ sound?: string /** diff --git a/plugins/notification/src/desktop.rs b/plugins/notification/src/desktop.rs index 47279225..4ceb8308 100644 --- a/plugins/notification/src/desktop.rs +++ b/plugins/notification/src/desktop.rs @@ -39,6 +39,9 @@ impl crate::NotificationBuilder { if let Some(icon) = self.data.icon { notification = notification.icon(icon); } + if let Some(sound) = self.data.sound { + notification = notification.sound(sound); + } #[cfg(feature = "windows7-compat")] { notification.notify(&self.app)?; @@ -102,6 +105,8 @@ mod imp { title: Option, /// The notification icon. icon: Option, + /// The notification sound. + sound: Option, /// The notification identifier identifier: String, } @@ -136,6 +141,13 @@ mod imp { self } + /// Sets the notification sound file. + #[must_use] + pub fn sound(mut self, sound: impl Into) -> Self { + self.sound = Some(sound.into()); + self + } + /// Shows the notification. /// /// # Examples @@ -177,6 +189,9 @@ mod imp { } else { notification.auto_icon(); } + if let Some(sound) = self.sound { + notification.sound_name(&sound); + } #[cfg(windows)] { let exe = tauri::utils::platform::current_exe()?; @@ -250,6 +265,7 @@ mod imp { } } + /// Shows the notification on Windows 7. #[cfg(all(windows, feature = "windows7-compat"))] fn notify_win7(self, app: &tauri::AppHandle) -> crate::Result<()> { let app_ = app.clone(); diff --git a/plugins/notification/src/lib.rs b/plugins/notification/src/lib.rs index 9ca33d63..8b79c873 100644 --- a/plugins/notification/src/lib.rs +++ b/plugins/notification/src/lib.rs @@ -132,7 +132,7 @@ impl NotificationBuilder { self } - /// The sound resource name. Only available on mobile. + /// The sound resource name for the notification. pub fn sound(mut self, sound: impl Into) -> Self { self.data.sound.replace(sound.into()); self