speakmore-2.0/static/js/notification.js

26 lines
755 B
JavaScript
Raw Normal View History

document.addEventListener('DOMContentLoaded', (event) => {
function requestNotificationPermission() {
if (Notification.permission !== "granted") {
Notification.requestPermission();
}
}
function showNotification(title, body) {
if (Notification.permission === "granted") {
new Notification(title, { body });
}
}
requestNotificationPermission();
function handleMessageNotification(data) {
const title = `New Message from ${data.sender}`;
const body = data.content.length > 100 ? data.content.substring(0, 97) + '...' : data.content;
showNotification(title, body);
}
window.notificationHandler = {
handleMessageNotification
};
});