feat: sound on browser push notification (#18548)
* feat: sound on notification * refactor: add load audio * chore: remove * fix: type error * fix: notification * fix: prevent multiple initialization = * feat: create notification page * chore: update the mp3 file
This commit is contained in:
1 parent
4daad2ac08
commit
b0125e1a05
16 files changed
+416
-237
No files matched your search
Binary file not shown.
@@ -1,4 +1,6 @@
|
||||
self.addEventListener("push", async (event) => {
|
||||
if (!event.data) return
|
||||
|
||||
let notificationData = event.data.json();
|
||||
|
||||
const allClients = await clients.matchAll({
|
||||
@@ -11,43 +13,82 @@ self.addEventListener("push", async (event) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const title = notificationData.title || "You have a new notification from Cal.com";
|
||||
const image = "https://cal.com/api/logo?type=icon";
|
||||
const newNotificationOptions = {
|
||||
requireInteraction: true,
|
||||
...notificationData,
|
||||
icon: image,
|
||||
badge: image,
|
||||
data: {
|
||||
url: notificationData.data?.url || "https://app.cal.com",
|
||||
},
|
||||
silent: false,
|
||||
vibrate: [300, 100, 400],
|
||||
tag: `notification-${Date.now()}-${Math.random()}`,
|
||||
};
|
||||
const title = notificationData.title || "New Cal.com Notification";
|
||||
const image = notificationData.icon || "https://cal.com/api/logo?type=icon";
|
||||
|
||||
const existingNotifications = await self.registration.getNotifications();
|
||||
event.waitUntil(
|
||||
(async () => {
|
||||
try {
|
||||
// Close notifications with the same tag if it exists
|
||||
const existingNotifications = await self.registration.getNotifications({
|
||||
tag: notificationData.tag
|
||||
});
|
||||
|
||||
// Display each existing notification again to make sure old ones can still be clicked
|
||||
existingNotifications.forEach((notification) => {
|
||||
const options = {
|
||||
body: notification.body,
|
||||
icon: notification.icon,
|
||||
badge: notification.badge,
|
||||
data: notification.data,
|
||||
silent: notification.silent,
|
||||
vibrate: notification.vibrate,
|
||||
requireInteraction: notification.requireInteraction,
|
||||
tag: notification.tag,
|
||||
};
|
||||
self.registration.showNotification(notification.title, options);
|
||||
});
|
||||
existingNotifications.forEach((notification) => {
|
||||
const options = {
|
||||
body: notification.body,
|
||||
icon: notification.icon,
|
||||
badge: notification.badge,
|
||||
data: notification.data,
|
||||
silent: notification.silent,
|
||||
vibrate: notification.vibrate,
|
||||
requireInteraction: notification.requireInteraction,
|
||||
tag: notification.tag,
|
||||
};
|
||||
|
||||
// Show the new notification
|
||||
self.registration.showNotification(title, newNotificationOptions);
|
||||
self.registration.showNotification(notification.title, options);
|
||||
});
|
||||
|
||||
// Special handling for instant meetings
|
||||
if (notificationData.data?.type === "INSTANT_MEETING") {
|
||||
allClients.forEach(client => {
|
||||
client.postMessage({
|
||||
type: 'PLAY_NOTIFICATION_SOUND'
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const notificationOptions = {
|
||||
body: notificationData.body,
|
||||
icon: image,
|
||||
badge: image,
|
||||
data: notificationData.data,
|
||||
tag: notificationData.tag || `cal-notification-${Date.now()}`,
|
||||
renotify: true,
|
||||
requireInteraction: notificationData.requireInteraction ?? true,
|
||||
actions: notificationData.actions || [],
|
||||
vibrate: [200, 100, 200],
|
||||
urgency: 'high'
|
||||
};
|
||||
|
||||
console.log("notificationOptions", notificationOptions);
|
||||
|
||||
await self.registration.showNotification(title, notificationOptions);
|
||||
console.log("Notification shown successfully");
|
||||
} catch (error) {
|
||||
console.error("Error showing notification:", error);
|
||||
}
|
||||
})()
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
self.addEventListener("notificationclick", (event) => {
|
||||
if (event.notification.data?.type === "INSTANT_MEETING") {
|
||||
const stopSound = async () => {
|
||||
const allClients = await clients.matchAll({
|
||||
type: "window",
|
||||
includeUncontrolled: true,
|
||||
});
|
||||
|
||||
allClients.forEach(client => {
|
||||
client.postMessage({
|
||||
type: 'STOP_NOTIFICATION_SOUND'
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
if (!event.action) {
|
||||
// Normal Notification Click
|
||||
event.notification.close();
|
||||
@@ -63,3 +104,11 @@ self.addEventListener("notificationclick", (event) => {
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
console.log('Service Worker installing.');
|
||||
});
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
console.log('Service Worker activated.');
|
||||
});
|
||||
@@ -494,6 +494,8 @@
|
||||
"browser_notifications_turned_off": "Browser Notifications turned off",
|
||||
"browser_notifications_denied": "Browser Notifications denied",
|
||||
"please_allow_notifications": "Please allow notifications from the prompt",
|
||||
"push_notifications": "Push Notifications",
|
||||
"push_notifications_description": "Receive push notifications when booker submits instant meeting booking.",
|
||||
"browser_notifications_not_supported": "Your browser does not support Push Notifications. If you are Brave user then enable `Use Google services for push messaging` Option on brave://settings/?search=push+messaging",
|
||||
"email": "Email",
|
||||
"email_placeholder": "[email protected]",
|
||||
|
||||
Reference in new issue
Block a user