Offline web push notifiion

Asked

Viewed 70 times

0

I’m creating a list to, and in it, I want to be able to create pre-programmed notifications. The application persists all data in Local Storage, IE, you do not have to use a push notification server

Then send messages to the Service Worker to manage notifications:

navigator.serviceWorker.register('./sw.js')
    .then(sw => {
        //Agenda uma futura notificação
        sw.active.postMessage({
            command: 'schedule',
            alert: { /* ... */ }
        });

        //Remove uma notificação agendada
        sw.active.postMessage({
            command: 'remove',
            alert: { /* ... */ }
        });
    })
    .catch(console.log);

And in Service Worker I use the setTimeout to schedule notifications

class Alert {
    /* ... */
}

const timer = {};

self.addEventListener('message', event => {
    switch (event.data.command) {
        case 'schedule':
            schedule(new Alert(event.data.alert));
            break;

        case 'remove':
            remove(event.data.alert.key);
            break;

        default:
            break;
    }
});

function remove(key) {
    if (timer[key]) {
        clearTimeout(timer[key]);

        delete timer[key];
    }
}

function schedule(alert) {
    if (!alert.canNotify()) {
        return;
    }

    remove(alert.key);

    timer[alert.key] = setTimeout(notify, alert.getTimeLeft(), alert.key, alert.title);
}

function notify(key, title) {
    self.registration.showNotification(title, {
        icon: '/assets/icons/android-chrome-192x192.png'
    });
}

It works, however, after notifying the user, I need to warn the application to update the information in Local Storage, since the Service Worker does not have access to it

I tried to send a message to clients in function notify:

self.clients.matchAll().then(
    clients => clients.forEach(
        client => {
            client.postMessage(key);
        }
    )
);

And in the application, update the data in the event message:

navigator.serviceWorker.addEventListener('message', event => {
    update(event.data);
});

But it only works if the application is open, which is a problem

How can I update this data?

  • localStorage will only be available if the application is "open" ... perhaps one should consider using indexedDB to operate in both scripts

  • @Lauromoraes may be the solution, but I would not like to have to use it because, besides being complicated, it does not persist the information forever

  • I didn’t understand the "forever" part but agree that it is a "less simple and less intuitive" API than localStorage

  • @Lauromoraes eventually the browser erases the entire Storage without any action on the part of the user or the application. The data is persisted but there is no guarantee that from time to time everything will disappear

  • Yes, but it affects localStorage tbm. Your application already works with a worker and notification you may want to look at the persistent mode: https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/persist ... but still the user or a plugin could clear the cache, for real (and resilient) pesistir only in the backend

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.