-
Notifications
You must be signed in to change notification settings - Fork 11.6k
feat: sound on browser push notification #18548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d7a3ab0
feat: sound on notification
Udit-takkar 9844359
refactor: add load audio
Udit-takkar f77ce1e
chore: remove
Udit-takkar ec6842e
fix: type error
Udit-takkar 7f19847
fix: notification
Udit-takkar 5279b4e
fix: prevent multiple initialization
Udit-takkar 52f1e5a
feat: create notification page
Udit-takkar 45eaeb1
chore: update the mp3 file
Udit-takkar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
apps/web/app/settings/(settings-layout)/my-account/push-notifications/page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { getTranslate } from "app/_utils"; | ||
| import { _generateMetadata } from "app/_utils"; | ||
|
|
||
| import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader"; | ||
|
|
||
| import PushNotificationsView from "~/settings/my-account/push-notifications-view"; | ||
|
|
||
| export const generateMetadata = async () => | ||
| await _generateMetadata( | ||
| (t) => t("push_notifications"), | ||
| (t) => t("push_notifications_description") | ||
| ); | ||
|
|
||
| const Page = async () => { | ||
| const t = await getTranslate(); | ||
|
|
||
| return ( | ||
| <SettingsHeader | ||
| title={t("push_notifications")} | ||
| description={t("push_notifications_description")} | ||
| borderInShellHeader={true}> | ||
| <PushNotificationsView /> | ||
| </SettingsHeader> | ||
| ); | ||
| }; | ||
|
|
||
| export default Page; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect, useRef } from "react"; | ||
|
|
||
| export function NotificationSoundHandler() { | ||
| const audioContextRef = useRef<AudioContext | null>(null); | ||
| const sourceRef = useRef<AudioBufferSourceNode | null>(null); | ||
| const audioBufferRef = useRef<AudioBuffer | null>(null); | ||
|
|
||
| const initializeAudioSystem = async () => { | ||
| try { | ||
| // Only create a new context if we don't have one or if it's closed | ||
| if (!audioContextRef.current || audioContextRef.current.state === "closed") { | ||
| audioContextRef.current = new (window.AudioContext || (window as any).webkitAudioContext)(); | ||
| } | ||
|
|
||
| if (audioContextRef.current.state === "suspended") { | ||
| await audioContextRef.current.resume(); | ||
| } | ||
|
|
||
| if (!audioBufferRef.current) { | ||
| const response = await fetch("/ring.mp3"); | ||
| const arrayBuffer = await response.arrayBuffer(); | ||
| audioBufferRef.current = await audioContextRef.current.decodeAudioData(arrayBuffer); | ||
| console.log("Audio file loaded and decoded"); | ||
| } | ||
|
|
||
| return true; | ||
| } catch (error) { | ||
| console.error("Failed to initialize audio system:", error); | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| const playSound = async () => { | ||
| try { | ||
| // Ensure audio system is initialized | ||
| if (!audioContextRef.current || audioContextRef.current.state === "closed") { | ||
| const initialized = await initializeAudioSystem(); | ||
| if (!initialized) return; | ||
| } | ||
|
|
||
| if (!audioContextRef.current) return; | ||
|
|
||
| // Resume context if suspended | ||
| if (audioContextRef.current.state === "suspended") { | ||
| await audioContextRef.current.resume(); | ||
| } | ||
|
|
||
| // Stop any currently playing sound | ||
| if (sourceRef.current) { | ||
| sourceRef.current.stop(); | ||
| sourceRef.current.disconnect(); | ||
| sourceRef.current = null; | ||
| } | ||
|
|
||
| // Create and play new sound | ||
| const source = audioContextRef.current.createBufferSource(); | ||
| source.buffer = audioBufferRef.current; | ||
| source.connect(audioContextRef.current.destination); | ||
| sourceRef.current = source; | ||
|
|
||
| source.loop = true; | ||
| source.start(0); | ||
| console.log("Sound started playing"); | ||
|
|
||
| setTimeout(() => { | ||
| if (sourceRef.current === source) { | ||
| source.stop(); | ||
| source.disconnect(); | ||
| sourceRef.current = null; | ||
| } | ||
| }, 7000); | ||
| } catch (error) { | ||
| console.error("Error playing sound:", error); | ||
| // Try to reinitialize on error | ||
| audioContextRef.current = null; | ||
| audioBufferRef.current = null; | ||
| } | ||
| }; | ||
|
|
||
| const stopSound = () => { | ||
| if (sourceRef.current) { | ||
| sourceRef.current.stop(); | ||
| sourceRef.current.disconnect(); | ||
| sourceRef.current = null; | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| // Don't automatically initialize - wait for user interaction | ||
| return () => { | ||
| stopSound(); | ||
| }; | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!("serviceWorker" in navigator)) { | ||
| console.log("ServiceWorker not available"); | ||
| return; | ||
| } | ||
|
|
||
| const messageHandler = async (event: MessageEvent) => { | ||
| if (event.data.type === "PLAY_NOTIFICATION_SOUND") { | ||
| // Only initialize if not already initialized | ||
| if (!audioBufferRef.current) { | ||
| await initializeAudioSystem(); | ||
| } | ||
| await playSound(); | ||
| } | ||
|
|
||
| if (event.data.type === "STOP_NOTIFICATION_SOUND") { | ||
| stopSound(); | ||
| } | ||
| }; | ||
|
|
||
| navigator.serviceWorker.addEventListener("message", messageHandler); | ||
| return () => navigator.serviceWorker.removeEventListener("message", messageHandler); | ||
| }, []); | ||
|
|
||
| // Single click handler for initial audio setup | ||
| useEffect(() => { | ||
| const handleFirstInteraction = async () => { | ||
| // Only initialize if not already initialized | ||
| if (!audioBufferRef.current) { | ||
| await initializeAudioSystem(); | ||
| } | ||
| document.removeEventListener("click", handleFirstInteraction); | ||
| document.removeEventListener("touchstart", handleFirstInteraction); | ||
| }; | ||
|
|
||
| document.addEventListener("click", handleFirstInteraction); | ||
| document.addEventListener("touchstart", handleFirstInteraction); | ||
|
|
||
| return () => { | ||
| document.removeEventListener("click", handleFirstInteraction); | ||
| document.removeEventListener("touchstart", handleFirstInteraction); | ||
| }; | ||
| }, []); | ||
|
|
||
| return null; | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
apps/web/modules/settings/my-account/push-notifications-view.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| "use client"; | ||
|
|
||
| import { useWebPush } from "@calcom/features/notifications/WebPushContext"; | ||
| import { useLocale } from "@calcom/lib/hooks/useLocale"; | ||
| import { Button } from "@calcom/ui"; | ||
|
|
||
| const PushNotificationsView = () => { | ||
| const { t } = useLocale(); | ||
| const { subscribe, unsubscribe, isSubscribed, isLoading } = useWebPush(); | ||
|
|
||
| return ( | ||
| <div className="border-subtle rounded-b-xl border-x border-b px-4 pb-10 pt-8 sm:px-6"> | ||
| <Button color="primary" onClick={isSubscribed ? unsubscribe : subscribe} disabled={isLoading}> | ||
| {isSubscribed ? t("disable_browser_notifications") : t("allow_browser_notifications")} | ||
| </Button> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default PushNotificationsView; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is required to load the audio as soon as user interacts with our site.
https://developer.chrome.com/blog/autoplay