pwa: push existing subscription at the beginning of each session (#85458)

This commit is contained in:
Benjamin Dauvergne 2024-01-09 22:25:22 +01:00
parent a5f8140d66
commit b2d06e0234
1 changed files with 10 additions and 1 deletions

View File

@ -21,7 +21,11 @@ if ('serviceWorker' in navigator) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
swRegistration = registration;
combo_pwa_initialize();
/* run pwa initialize after page loading, so that pwa-user-info event can
be handled by the notification cell handler. */
$(function () {
combo_pwa_initialize();
})
}).catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
@ -33,8 +37,12 @@ function combo_pwa_initialize() {
swRegistration.pushManager.getSubscription()
.then(function(subscription) {
if (subscription !== null) {
if (sessionStorage.getItem('push-subscription')) {
combo_pwa_update_subscription_on_server(subscription);
}
COMBO_PWA_USER_SUBSCRIPTION = true;
} else {
sessionStorage.removeItem('push-subscription');
COMBO_PWA_USER_SUBSCRIPTION = false;
}
$(document).trigger('combo:pwa-user-info');
@ -85,6 +93,7 @@ function combo_pwa_update_subscription_on_server(subscription) {
type: 'POST',
dataType: 'json',
success: function(response) {
sessionStorage.setItem('push-subscription', 'registered')
}
});
}