mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-06 13:53:54 +02:00
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 <lucas@tauri.app>
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"notification": patch
|
||||||
|
"notification-js": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Added sound support for desktop notifications which was previously only available on mobile platforms.
|
||||||
@@ -1,16 +1,21 @@
|
|||||||
<script>
|
<script>
|
||||||
|
import { sendNotification } from '@tauri-apps/plugin-notification'
|
||||||
export let onMessage
|
export let onMessage
|
||||||
|
|
||||||
|
let sound = ''
|
||||||
|
|
||||||
// send the notification directly
|
// send the notification directly
|
||||||
// the backend is responsible for checking the permission
|
// the backend is responsible for checking the permission
|
||||||
function _sendNotification() {
|
function _sendNotification() {
|
||||||
new Notification('Notification title', {
|
sendNotification({
|
||||||
body: 'This is the notification body'
|
title: 'Notification title',
|
||||||
|
body: 'This is the notification body',
|
||||||
|
sound: sound || null
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// alternatively, check the permission ourselves
|
// alternatively, check the permission ourselves
|
||||||
function sendNotification() {
|
function triggerNotification() {
|
||||||
if (Notification.permission === 'default') {
|
if (Notification.permission === 'default') {
|
||||||
Notification.requestPermission()
|
Notification.requestPermission()
|
||||||
.then(function (response) {
|
.then(function (response) {
|
||||||
@@ -29,6 +34,11 @@
|
|||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<button class="btn" id="notification" on:click={sendNotification}>
|
<input
|
||||||
|
class="input grow"
|
||||||
|
placeholder="Notification sound..."
|
||||||
|
bind:value={sound}
|
||||||
|
/>
|
||||||
|
<button class="btn" id="notification" on:click={triggerNotification}>
|
||||||
Send test notification
|
Send test notification
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -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
|
## Contributing
|
||||||
|
|
||||||
PRs accepted. Please make sure to read the Contributing Guide before making a pull request.
|
PRs accepted. Please make sure to read the Contributing Guide before making a pull request.
|
||||||
|
|||||||
@@ -71,7 +71,13 @@ interface Options {
|
|||||||
*/
|
*/
|
||||||
groupSummary?: boolean
|
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
|
sound?: string
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ impl<R: Runtime> crate::NotificationBuilder<R> {
|
|||||||
if let Some(icon) = self.data.icon {
|
if let Some(icon) = self.data.icon {
|
||||||
notification = notification.icon(icon);
|
notification = notification.icon(icon);
|
||||||
}
|
}
|
||||||
|
if let Some(sound) = self.data.sound {
|
||||||
|
notification = notification.sound(sound);
|
||||||
|
}
|
||||||
#[cfg(feature = "windows7-compat")]
|
#[cfg(feature = "windows7-compat")]
|
||||||
{
|
{
|
||||||
notification.notify(&self.app)?;
|
notification.notify(&self.app)?;
|
||||||
@@ -102,6 +105,8 @@ mod imp {
|
|||||||
title: Option<String>,
|
title: Option<String>,
|
||||||
/// The notification icon.
|
/// The notification icon.
|
||||||
icon: Option<String>,
|
icon: Option<String>,
|
||||||
|
/// The notification sound.
|
||||||
|
sound: Option<String>,
|
||||||
/// The notification identifier
|
/// The notification identifier
|
||||||
identifier: String,
|
identifier: String,
|
||||||
}
|
}
|
||||||
@@ -136,6 +141,13 @@ mod imp {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the notification sound file.
|
||||||
|
#[must_use]
|
||||||
|
pub fn sound(mut self, sound: impl Into<String>) -> Self {
|
||||||
|
self.sound = Some(sound.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Shows the notification.
|
/// Shows the notification.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
@@ -177,6 +189,9 @@ mod imp {
|
|||||||
} else {
|
} else {
|
||||||
notification.auto_icon();
|
notification.auto_icon();
|
||||||
}
|
}
|
||||||
|
if let Some(sound) = self.sound {
|
||||||
|
notification.sound_name(&sound);
|
||||||
|
}
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
{
|
{
|
||||||
let exe = tauri::utils::platform::current_exe()?;
|
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"))]
|
#[cfg(all(windows, feature = "windows7-compat"))]
|
||||||
fn notify_win7<R: tauri::Runtime>(self, app: &tauri::AppHandle<R>) -> crate::Result<()> {
|
fn notify_win7<R: tauri::Runtime>(self, app: &tauri::AppHandle<R>) -> crate::Result<()> {
|
||||||
let app_ = app.clone();
|
let app_ = app.clone();
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ impl<R: Runtime> NotificationBuilder<R> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The sound resource name. Only available on mobile.
|
/// The sound resource name for the notification.
|
||||||
pub fn sound(mut self, sound: impl Into<String>) -> Self {
|
pub fn sound(mut self, sound: impl Into<String>) -> Self {
|
||||||
self.data.sound.replace(sound.into());
|
self.data.sound.replace(sound.into());
|
||||||
self
|
self
|
||||||
|
|||||||
Reference in New Issue
Block a user