@@ -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 ( ) {
0 commit comments