Files
tauri-plugins-workspace/examples/api/src/views/Notifications.svelte
T
Keerthi 2d03e2eac2 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>
2025-08-21 10:05:27 -03:00

45 lines
1.1 KiB
Svelte

<script>
import { sendNotification } from '@tauri-apps/plugin-notification'
export let onMessage
let sound = ''
// send the notification directly
// the backend is responsible for checking the permission
function _sendNotification() {
sendNotification({
title: 'Notification title',
body: 'This is the notification body',
sound: sound || null
})
}
// alternatively, check the permission ourselves
function triggerNotification() {
if (Notification.permission === 'default') {
Notification.requestPermission()
.then(function (response) {
if (response === 'granted') {
_sendNotification()
} else {
onMessage('Permission is ' + response)
}
})
.catch(onMessage)
} else if (Notification.permission === 'granted') {
_sendNotification()
} else {
onMessage('Permission is denied')
}
}
</script>
<input
class="input grow"
placeholder="Notification sound..."
bind:value={sound}
/>
<button class="btn" id="notification" on:click={triggerNotification}>
Send test notification
</button>