diff --git a/phpBB/phpbb/notification/method/webpush.php b/phpBB/phpbb/notification/method/webpush.php index e5b8910a07..271e23dffe 100644 --- a/phpBB/phpbb/notification/method/webpush.php +++ b/phpBB/phpbb/notification/method/webpush.php @@ -210,6 +210,7 @@ class webpush extends messenger_base implements extended_method_interface $data = [ 'item_id' => $notification->item_id, 'type_id' => $notification->notification_type_id, + 'version' => $this->config['assets_version'], ]; $json_data = json_encode($data); diff --git a/phpBB/phpbb/ucp/controller/webpush.php b/phpBB/phpbb/ucp/controller/webpush.php index a7ebbffae8..b60fa437b3 100644 --- a/phpBB/phpbb/ucp/controller/webpush.php +++ b/phpBB/phpbb/ucp/controller/webpush.php @@ -138,6 +138,7 @@ class webpush // @todo: only work for logged in users, no anonymous & bot $content = $this->template->render('push_worker.js.twig', [ 'U_WEBPUSH_GET_NOTIFICATION' => $this->controller_helper->route('phpbb_ucp_push_get_notification_controller'), + 'ASSETS_VERSION' => $this->config['assets_version'], ]); $response = new Response($content); diff --git a/phpBB/styles/all/js/push_worker.js.twig b/phpBB/styles/all/js/push_worker.js.twig index 8d6ec3c6af..f2807cb43a 100644 --- a/phpBB/styles/all/js/push_worker.js.twig +++ b/phpBB/styles/all/js/push_worker.js.twig @@ -1,3 +1,18 @@ +/** + * Event listener for install event + */ +self.addEventListener('install', () => { + // Call to ensure service worker is correctly updated + self.skipWaiting(); +}); + +/** + * Event listener for activate event + */ +self.addEventListener('activate', event => { + event.waitUntil(self.clients.claim()); +}); + /** * Event listener for push event */ @@ -7,17 +22,25 @@ self.addEventListener('push', event => { } let itemId = 0; - let typeId = 0; + let typeId = 0; + let notificationVersion = 5; try { const notificationData = event.data.json(); itemId = notificationData.item_id; typeId = notificationData.type_id; + notificationVersion = parseInt(notificationData.version, 10); } catch { self.registration.showNotification(event.data.text()); return; } const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}'; + const assetsVersion = parseInt('{{ ASSETS_VERSION }}', 10); + + // Force update if versions differ + if (assetsVersion !== notificationVersion) { + self.registration.update(); + } const formData = new FormData(); formData.append('item_id', itemId.toString(10));