Skip to content

Commit 2d03e2e

Browse files
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 <[email protected]>
1 parent 21d721a commit 2d03e2e

File tree

6 files changed

+83
-6
lines changed

6 files changed

+83
-6
lines changed

.changes/notification-sound.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"notification": patch
3+
"notification-js": patch
4+
---
5+
6+
Added sound support for desktop notifications which was previously only available on mobile platforms.

examples/api/src/views/Notifications.svelte

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
<script>
2+
import { sendNotification } from '@tauri-apps/plugin-notification'
23
export let onMessage
34
5+
let sound = ''
6+
47
// send the notification directly
58
// the backend is responsible for checking the permission
69
function _sendNotification() {
7-
new Notification('Notification title', {
8-
body: 'This is the notification body'
10+
sendNotification({
11+
title: 'Notification title',
12+
body: 'This is the notification body',
13+
sound: sound || null
914
})
1015
}
1116
1217
// alternatively, check the permission ourselves
13-
function sendNotification() {
18+
function triggerNotification() {
1419
if (Notification.permission === 'default') {
1520
Notification.requestPermission()
1621
.then(function (response) {
@@ -29,6 +34,11 @@
2934
}
3035
</script>
3136

32-
<button class="btn" id="notification" on:click={sendNotification}>
37+
<input
38+
class="input grow"
39+
placeholder="Notification sound..."
40+
bind:value={sound}
41+
/>
42+
<button class="btn" id="notification" on:click={triggerNotification}>
3343
Send test notification
3444
</button>

plugins/notification/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,45 @@ export async function enqueueNotification(title, body) {
9595
}
9696
```
9797

98+
### Notification with Sound
99+
100+
You can add sound to your notifications on all platforms (desktop and mobile):
101+
102+
```javascript
103+
import { sendNotification } from '@tauri-apps/plugin-notification'
104+
import { platform } from '@tauri-apps/api/os'
105+
106+
// Basic notification with sound
107+
sendNotification({
108+
title: 'New Message',
109+
body: 'You have a new message',
110+
sound: 'notification.wav' // Path to sound file
111+
})
112+
113+
// Platform-specific sounds
114+
async function sendPlatformSpecificNotification() {
115+
const platformName = platform()
116+
117+
let soundPath
118+
if (platformName === 'darwin') {
119+
// On macOS: use system sounds or sound files in the app bundle
120+
soundPath = 'Ping' // macOS system sound
121+
} else if (platformName === 'linux') {
122+
// On Linux: use XDG theme sounds or file paths
123+
soundPath = 'message-new-instant' // XDG theme sound
124+
} else {
125+
// On Windows: use file paths
126+
soundPath = 'notification.wav'
127+
}
128+
129+
sendNotification({
130+
title: 'Platform-specific Notification',
131+
body: 'This notification uses platform-specific sound',
132+
sound: soundPath
133+
})
134+
}
135+
```
136+
98137
## Contributing
99138

100139
PRs accepted. Please make sure to read the Contributing Guide before making a pull request.

plugins/notification/guest-js/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ interface Options {
7171
*/
7272
groupSummary?: boolean
7373
/**
74-
* The sound resource name. Only available on mobile.
74+
* The sound resource name or file path for the notification.
75+
*
76+
* Platform specific behavior:
77+
* - On macOS: use system sounds (e.g., "Ping", "Blow") or sound files in the app bundle
78+
* - On Linux: use XDG theme sounds (e.g., "message-new-instant") or file paths
79+
* - On Windows: use file paths to sound files (.wav format)
80+
* - On Mobile: use resource names
7581
*/
7682
sound?: string
7783
/**

plugins/notification/src/desktop.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ impl<R: Runtime> crate::NotificationBuilder<R> {
3939
if let Some(icon) = self.data.icon {
4040
notification = notification.icon(icon);
4141
}
42+
if let Some(sound) = self.data.sound {
43+
notification = notification.sound(sound);
44+
}
4245
#[cfg(feature = "windows7-compat")]
4346
{
4447
notification.notify(&self.app)?;
@@ -102,6 +105,8 @@ mod imp {
102105
title: Option<String>,
103106
/// The notification icon.
104107
icon: Option<String>,
108+
/// The notification sound.
109+
sound: Option<String>,
105110
/// The notification identifier
106111
identifier: String,
107112
}
@@ -136,6 +141,13 @@ mod imp {
136141
self
137142
}
138143

144+
/// Sets the notification sound file.
145+
#[must_use]
146+
pub fn sound(mut self, sound: impl Into<String>) -> Self {
147+
self.sound = Some(sound.into());
148+
self
149+
}
150+
139151
/// Shows the notification.
140152
///
141153
/// # Examples
@@ -177,6 +189,9 @@ mod imp {
177189
} else {
178190
notification.auto_icon();
179191
}
192+
if let Some(sound) = self.sound {
193+
notification.sound_name(&sound);
194+
}
180195
#[cfg(windows)]
181196
{
182197
let exe = tauri::utils::platform::current_exe()?;
@@ -250,6 +265,7 @@ mod imp {
250265
}
251266
}
252267

268+
/// Shows the notification on Windows 7.
253269
#[cfg(all(windows, feature = "windows7-compat"))]
254270
fn notify_win7<R: tauri::Runtime>(self, app: &tauri::AppHandle<R>) -> crate::Result<()> {
255271
let app_ = app.clone();

plugins/notification/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<R: Runtime> NotificationBuilder<R> {
132132
self
133133
}
134134

135-
/// The sound resource name. Only available on mobile.
135+
/// The sound resource name for the notification.
136136
pub fn sound(mut self, sound: impl Into<String>) -> Self {
137137
self.data.sound.replace(sound.into());
138138
self

0 commit comments

Comments
 (0)