flotilla/src/app/components/NewNotificationSound.svelte
Coracle-Bot f15f3b9c95 Never play the notification sound on native platforms (#406)
Co-authored-by: Coracle-Bot <npub1klq6260@nostr.local>
2026-09-02 22:59:56 +00:00

39 lines
911 B
Svelte

<script lang="ts">
import {onMount} from "svelte"
import {Capacitor} from "@capacitor/core"
import {notificationSettings} from "@app/settings"
import {onNotification} from "@app/push"
let audioElement: HTMLAudioElement
let enabled = $state(false)
const onVisibilityChange = () => {
if (document.hidden) {
enabled = true
} else {
enabled = false
}
}
onMount(() => {
if (Capacitor.isNativePlatform()) return
audioElement.load()
document.addEventListener("visibilitychange", onVisibilityChange)
const unsubscribe = onNotification(() => {
if (enabled && $notificationSettings.sound) {
audioElement?.play()
}
})
return () => {
unsubscribe()
document.removeEventListener("visibilitychange", onVisibilityChange)
}
})
</script>
<audio bind:this={audioElement} src="/new-notification-3-398649.mp3"></audio>