parent
e7d1dbe849
commit
37986f8b0b
1 changed files with 32 additions and 4 deletions
|
|
@ -19,7 +19,8 @@ const state = {
|
||||||
isEditing: false,
|
isEditing: false,
|
||||||
isAuthenticated: false,
|
isAuthenticated: false,
|
||||||
chatMessages: [],
|
chatMessages: [],
|
||||||
activeDashboardTab: 'chat' // 'chat' or 'activity'
|
activeDashboardTab: 'chat', // 'chat' or 'activity'
|
||||||
|
swRegistration: null
|
||||||
};
|
};
|
||||||
// DOM elements
|
// DOM elements
|
||||||
const loginOverlay = document.getElementById('login-overlay');
|
const loginOverlay = document.getElementById('login-overlay');
|
||||||
|
|
@ -825,7 +826,7 @@ if ('serviceWorker' in navigator) {
|
||||||
navigator.serviceWorker.register('/sw.js')
|
navigator.serviceWorker.register('/sw.js')
|
||||||
.then(reg => {
|
.then(reg => {
|
||||||
console.log('SW registered', reg);
|
console.log('SW registered', reg);
|
||||||
setupPush(reg);
|
state.swRegistration = reg;
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error('SW registration failed:', err);
|
console.error('SW registration failed:', err);
|
||||||
|
|
@ -836,12 +837,17 @@ if ('serviceWorker' in navigator) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setupPush(registration) {
|
async function setupPush() {
|
||||||
|
if (!state.swRegistration) {
|
||||||
|
console.warn('SW registration not available');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const vapidResponse = await fetch(`${API_URL}/notifications/vapid-key`);
|
const vapidResponse = await fetch(`${API_URL}/notifications/vapid-key`);
|
||||||
const { publicKey } = await vapidResponse.json();
|
const { publicKey } = await vapidResponse.json();
|
||||||
|
|
||||||
const subscription = await registration.pushManager.subscribe({
|
const subscription = await state.swRegistration.pushManager.subscribe({
|
||||||
userVisibleOnly: true,
|
userVisibleOnly: true,
|
||||||
applicationServerKey: urlBase64ToUint8Array(publicKey)
|
applicationServerKey: urlBase64ToUint8Array(publicKey)
|
||||||
});
|
});
|
||||||
|
|
@ -856,8 +862,10 @@ async function setupPush(registration) {
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
console.log('Push registered');
|
console.log('Push registered');
|
||||||
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('Push registration failed:', err);
|
console.warn('Push registration failed:', err);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -881,6 +889,26 @@ async function toggleTaskSubscription(taskId) {
|
||||||
const isNotified = btn.classList.contains('notified');
|
const isNotified = btn.classList.contains('notified');
|
||||||
const method = isNotified ? 'DELETE' : 'POST';
|
const method = isNotified ? 'DELETE' : 'POST';
|
||||||
|
|
||||||
|
// If trying to enable but no push subscription, try setting it up first (user gesture here)
|
||||||
|
if (!isNotified && 'Notification' in window) {
|
||||||
|
if (Notification.permission !== 'granted') {
|
||||||
|
const permission = await Notification.requestPermission();
|
||||||
|
if (permission !== 'granted') {
|
||||||
|
showToast('Notification permission denied', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const sub = await state.swRegistration.pushManager.getSubscription();
|
||||||
|
if (!sub) {
|
||||||
|
const success = await setupPush();
|
||||||
|
if (!success) {
|
||||||
|
showToast('Failed to initialize push notifications', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fetch(`${API_URL}/tasks/${taskId}/subscribe`, { method });
|
await fetch(`${API_URL}/tasks/${taskId}/subscribe`, { method });
|
||||||
btn.classList.toggle('notified');
|
btn.classList.toggle('notified');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue