diff --git a/event/listener.php b/event/listener.php index 203d58e..c05ad5e 100644 --- a/event/listener.php +++ b/event/listener.php @@ -30,6 +30,7 @@ public static function getSubscribedEvents() 'core.ucp_notifications_output_notification_types_modify_template_vars' => 'load_template_data', 'core.ucp_display_module_before' => 'load_language', 'core.acp_main_notice' => 'compatibility_notice', + 'core.page_header_after' => 'service_worker_url', ]; } @@ -90,4 +91,15 @@ public function compatibility_notice() { $this->template->assign_var('S_WPN_COMPATIBILITY_NOTICE', phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '>=')); } + + /** + * Generate service worker URL globally for update + */ + public function service_worker_url() + { + if (!$this->template->retrieve_var('U_WEBPUSH_WORKER_URL')) + { + $this->template->assign_var('U_WEBPUSH_WORKER_URL', $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller')); + } + } } diff --git a/notification/method/webpush.php b/notification/method/webpush.php index 295b12b..8e74fbb 100644 --- a/notification/method/webpush.php +++ b/notification/method/webpush.php @@ -445,7 +445,7 @@ protected function clean_expired_subscriptions(array $user_subscription_map, arr * into https://myboard.url/path/to/avatar=123456789.gif * * @param string $avatar - * @return string Absolute path to avatar image + * @return array 'src' => Absolute path to avatar image */ protected function prepare_avatar($avatar) { @@ -455,7 +455,7 @@ protected function prepare_avatar($avatar) $path = !empty($matches[1]) ? end($matches[1]) : $avatar; - return preg_replace('#^' . preg_quote($this->path_helper->get_web_root_path(), '#') . '#', $this->get_board_url(), $path, 1); + return ['src' => preg_replace('#^' . preg_quote($this->path_helper->get_web_root_path(), '#') . '#', $this->get_board_url(), $path, 1)]; } /** diff --git a/styles/all/template/push_worker.js.twig b/styles/all/template/push_worker.js.twig index 428c211..8d6ec3c 100644 --- a/styles/all/template/push_worker.js.twig +++ b/styles/all/template/push_worker.js.twig @@ -36,7 +36,7 @@ self.addEventListener('push', event => { const options = { body: responseBody, data: response, - icon: response.avatar, + icon: response.avatar.src, }; self.registration.showNotification(response.heading, options); }); diff --git a/styles/all/template/update_worker.js b/styles/all/template/update_worker.js new file mode 100644 index 0000000..11bb989 --- /dev/null +++ b/styles/all/template/update_worker.js @@ -0,0 +1,29 @@ +'use strict'; + +function webpushWorkerUpdate() { + if ('serviceWorker' in navigator) { + navigator.serviceWorker.getRegistration(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(() => { + /* global serviceWorkerUrl */ + webpushWorkerUpdate(); +}); diff --git a/styles/prosilver/template/event/overall_footer_after.html b/styles/prosilver/template/event/overall_footer_after.html new file mode 100644 index 0000000..9249944 --- /dev/null +++ b/styles/prosilver/template/event/overall_footer_after.html @@ -0,0 +1,4 @@ + +{% INCLUDEJS('@phpbb_webpushnotifications/update_worker.js') %} diff --git a/tests/event/listener_test.php b/tests/event/listener_test.php index 3045ad6..b226de9 100644 --- a/tests/event/listener_test.php +++ b/tests/event/listener_test.php @@ -117,6 +117,7 @@ public function test_getSubscribedEvents() 'core.ucp_notifications_output_notification_types_modify_template_vars', 'core.ucp_display_module_before', 'core.acp_main_notice', + 'core.page_header_after', ], array_keys(\phpbb\webpushnotifications\event\listener::getSubscribedEvents())); }