Skip to content

Commit 4b0a2ed

Browse files
authored
Merge pull request #38 from rxu/update-push-worker
Provide correct push worker update.
2 parents 1defba2 + c94e233 commit 4b0a2ed

6 files changed

Lines changed: 49 additions & 3 deletions

File tree

event/listener.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static function getSubscribedEvents()
3030
'core.ucp_notifications_output_notification_types_modify_template_vars' => 'load_template_data',
3131
'core.ucp_display_module_before' => 'load_language',
3232
'core.acp_main_notice' => 'compatibility_notice',
33+
'core.page_header_after' => 'service_worker_url',
3334
];
3435
}
3536

@@ -90,4 +91,15 @@ public function compatibility_notice()
9091
{
9192
$this->template->assign_var('S_WPN_COMPATIBILITY_NOTICE', phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '>='));
9293
}
94+
95+
/**
96+
* Generate service worker URL globally for update
97+
*/
98+
public function service_worker_url()
99+
{
100+
if (!$this->template->retrieve_var('U_WEBPUSH_WORKER_URL'))
101+
{
102+
$this->template->assign_var('U_WEBPUSH_WORKER_URL', $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'));
103+
}
104+
}
93105
}

notification/method/webpush.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ protected function clean_expired_subscriptions(array $user_subscription_map, arr
445445
* into https://myboard.url/path/to/avatar=123456789.gif
446446
*
447447
* @param string $avatar
448-
* @return string Absolute path to avatar image
448+
* @return array 'src' => Absolute path to avatar image
449449
*/
450450
protected function prepare_avatar($avatar)
451451
{
@@ -455,7 +455,7 @@ protected function prepare_avatar($avatar)
455455

456456
$path = !empty($matches[1]) ? end($matches[1]) : $avatar;
457457

458-
return preg_replace('#^' . preg_quote($this->path_helper->get_web_root_path(), '#') . '#', $this->get_board_url(), $path, 1);
458+
return ['src' => preg_replace('#^' . preg_quote($this->path_helper->get_web_root_path(), '#') . '#', $this->get_board_url(), $path, 1)];
459459
}
460460

461461
/**

styles/all/template/push_worker.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ self.addEventListener('push', event => {
3636
const options = {
3737
body: responseBody,
3838
data: response,
39-
icon: response.avatar,
39+
icon: response.avatar.src,
4040
};
4141
self.registration.showNotification(response.heading, options);
4242
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
function webpushWorkerUpdate() {
4+
if ('serviceWorker' in navigator) {
5+
navigator.serviceWorker.getRegistration(serviceWorkerUrl)
6+
.then((registration) => {
7+
registration.update();
8+
})
9+
.catch(error => {
10+
// Service worker could not be updated
11+
console.info(error);
12+
});
13+
}
14+
}
15+
// Do not redeclare function if exist
16+
if (typeof domReady === 'undefined') {
17+
window.domReady = function(callBack) {
18+
if (document.readyState === 'loading') {
19+
document.addEventListener('DOMContentLoaded', callBack);
20+
} else {
21+
callBack();
22+
}
23+
}
24+
}
25+
26+
domReady(() => {
27+
/* global serviceWorkerUrl */
28+
webpushWorkerUpdate();
29+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script>
2+
const serviceWorkerUrl = '{{ U_WEBPUSH_WORKER_URL }}';
3+
</script>
4+
{% INCLUDEJS('@phpbb_webpushnotifications/update_worker.js') %}

tests/event/listener_test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function test_getSubscribedEvents()
117117
'core.ucp_notifications_output_notification_types_modify_template_vars',
118118
'core.ucp_display_module_before',
119119
'core.acp_main_notice',
120+
'core.page_header_after',
120121
], array_keys(\phpbb\webpushnotifications\event\listener::getSubscribedEvents()));
121122
}
122123

0 commit comments

Comments
 (0)