diff --git a/config/services.yml b/config/services.yml index c3e892b..1f94b7b 100644 --- a/config/services.yml +++ b/config/services.yml @@ -40,6 +40,7 @@ services: phpbb.wpn.ucp.controller.webpush: class: phpbb\webpushnotifications\ucp\controller\webpush arguments: + - '@config' - '@controller.helper' - '@dbal.conn' - '@phpbb.wpn.form_helper' diff --git a/notification/method/webpush.php b/notification/method/webpush.php index aba2e37..847a606 100644 --- a/notification/method/webpush.php +++ b/notification/method/webpush.php @@ -221,6 +221,7 @@ protected function notify_using_webpush(): void $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/styles/all/template/push_worker.js.twig b/styles/all/template/push_worker.js.twig index 8d6ec3c..f2807cb 100644 --- a/styles/all/template/push_worker.js.twig +++ b/styles/all/template/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)); diff --git a/styles/all/template/ucp_notifications_webpush.html b/styles/all/template/ucp_notifications_webpush.html index c93cebb..16ebf96 100644 --- a/styles/all/template/ucp_notifications_webpush.html +++ b/styles/all/template/ucp_notifications_webpush.html @@ -18,5 +18,4 @@ {% INCLUDEJS '@phpbb_webpushnotifications/webpush.js' %} -{% INCLUDEJS '@phpbb_webpushnotifications/update_worker.js' %} {% INCLUDECSS '@phpbb_webpushnotifications/phpbb_wpn.css' %} diff --git a/styles/all/template/update_worker.js b/styles/all/template/update_worker.js deleted file mode 100644 index 937c3be..0000000 --- a/styles/all/template/update_worker.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict'; - -/* global phpbbWebpushOptions, domReady */ -function webpushWorkerUpdate() { - if ('serviceWorker' in navigator) { - navigator.serviceWorker.getRegistration(phpbbWebpushOptions.serviceWorkerUrl) - .then((registration) => { - registration.update(); - }) - .catch(error => { - // Service worker could not be updated - console.info(error); - }); - } -} -// Do not redeclare function if exist -if (typeof domReady === 'undefined') { - window.domReady = function(callBack) { - if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', callBack); - } else { - callBack(); - } - }; -} - -domReady(() => { - webpushWorkerUpdate(); -}); diff --git a/ucp/controller/webpush.php b/ucp/controller/webpush.php index 77406db..9d8eb63 100644 --- a/ucp/controller/webpush.php +++ b/ucp/controller/webpush.php @@ -10,6 +10,7 @@ namespace phpbb\webpushnotifications\ucp\controller; +use phpbb\config\config; use phpbb\controller\helper as controller_helper; use phpbb\db\driver\driver_interface; use phpbb\exception\http_exception; @@ -31,6 +32,9 @@ class webpush /** @var string UCP form token name */ public const FORM_TOKEN_UCP = 'ucp_webpush'; + /** @var config */ + protected $config; + /** @var controller_helper */ protected $controller_helper; @@ -61,6 +65,7 @@ class webpush /** * Constructor for webpush controller * + * @param config $config * @param controller_helper $controller_helper * @param driver_interface $db * @param form_helper $form_helper @@ -71,9 +76,10 @@ class webpush * @param string $notification_webpush_table * @param string $push_subscriptions_table */ - public function __construct(controller_helper $controller_helper, driver_interface $db, form_helper $form_helper, path_helper $path_helper, + public function __construct(config $config, controller_helper $controller_helper, driver_interface $db, form_helper $form_helper, path_helper $path_helper, request_interface $request, user $user, Environment $template, string $notification_webpush_table, string $push_subscriptions_table) { + $this->config = $config; $this->controller_helper = $controller_helper; $this->db = $db; $this->form_helper = $form_helper; @@ -129,6 +135,7 @@ public function worker(): Response // @todo: only work for logged in users, no anonymous & bot $content = $this->template->render('@phpbb_webpushnotifications/push_worker.js.twig', [ 'U_WEBPUSH_GET_NOTIFICATION' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_get_notification_controller'), + 'ASSETS_VERSION' => $this->config['assets_version'], ]); $response = new Response($content);