Skip to content

Commit 22b5d13

Browse files
author
tung-ideapad
committed
notif SW
1 parent 830df79 commit 22b5d13

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

src/index.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,10 @@ document.addEventListener('alpine:init', () => { Alpine.data('mainApp', () => ({
423423
const now = new Date().getTime();
424424
const delay = reminderTime - now;
425425

426-
console.log('scheduleNotification', new Date(reminderTime), new Date(), delay);
426+
console.log('scheduleNotification', new Date(reminderTime), new Date(), delay, 'ms');
427427

428428
if (delay > 0) {
429-
const timeoutId = setTimeout(() => {
429+
this.scheduledNotifications[note.id] = setTimeout(() => {
430430
navigator.serviceWorker.ready.then(registration => {
431431
registration.showNotification(note.title, {
432432
body: note.content.substring(0, 100),
@@ -436,9 +436,21 @@ document.addEventListener('alpine:init', () => { Alpine.data('mainApp', () => ({
436436
});
437437
});
438438
}, delay);
439-
440-
this.scheduledNotifications[note.id] = timeoutId;
441439
console.log(`Reminder scheduled for note "${note.title}" in ${Math.floor(delay / 60e3)} minutes`);
440+
441+
navigator.serviceWorker.ready.then(registration => {
442+
if (registration.active) {
443+
registration.active.postMessage({
444+
action: 'SCHEDULE_NOTIFICATION',
445+
id: note.id,
446+
title: note.title,
447+
content: note.content ? note.content.substring(0, 100) : '',
448+
delay: delay,
449+
url: `/#note/${note.id}`
450+
});
451+
console.log(`Reminder scheduled (SW) for note "${note.title}" in ${Math.floor(delay / 60e3)} minutes`);
452+
}
453+
});
442454
}
443455
},
444456

@@ -448,6 +460,16 @@ document.addEventListener('alpine:init', () => { Alpine.data('mainApp', () => ({
448460
delete this.scheduledNotifications[noteId];
449461
console.log(`Cancelled reminder for note ${noteId}`);
450462
}
463+
464+
navigator.serviceWorker.ready.then(registration => {
465+
if (registration.active) {
466+
registration.active.postMessage({
467+
action: 'CANCEL_NOTIFICATION',
468+
id: noteId
469+
});
470+
}
471+
});
472+
console.log(`Cancelled reminder (SW) for note ${noteId}`);
451473
},
452474

453475
scheduleAllFutureReminders() {

src/serviceworker.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,44 @@ self.addEventListener('activate', (event) => {
267267

268268

269269

270+
271+
const scheduledNotifications = new Map();
272+
273+
self.addEventListener('message', (event) => {
274+
if (!event.data) return;
275+
276+
if (event.data.action === 'SCHEDULE_NOTIFICATION') {
277+
const { id, title, content, delay, url } = event.data;
278+
279+
// Cancel existing if any
280+
if (scheduledNotifications.has(id)) {
281+
clearTimeout(scheduledNotifications.get(id));
282+
scheduledNotifications.delete(id);
283+
}
284+
285+
if (delay > 0) {
286+
const timeoutId = setTimeout(() => {
287+
self.registration.showNotification(title, {
288+
body: content,
289+
icon: '/favicon.png',
290+
badge: '/favicon.png',
291+
data: { url: url }
292+
});
293+
scheduledNotifications.delete(id);
294+
}, delay);
295+
scheduledNotifications.set(id, timeoutId);
296+
console.log(`SW: Scheduled notification for note ${id} in ${Math.floor(delay / 1000)}s`);
297+
}
298+
} else if (event.data.action === 'CANCEL_NOTIFICATION') {
299+
const { id } = event.data;
300+
if (scheduledNotifications.has(id)) {
301+
clearTimeout(scheduledNotifications.get(id));
302+
scheduledNotifications.delete(id);
303+
console.log(`SW: Cancelled notification for note ${id}`);
304+
}
305+
}
306+
});
307+
270308
self.addEventListener('notificationclick', (event) => {
271309
event.notification.close();
272310
const urlToOpen = event.notification.data.url || '/';

0 commit comments

Comments
 (0)