Skip to content

Commit f4752d6

Browse files
authored
Merge pull request #46 from iMattPro/update-worker
Update worker
2 parents 689dde6 + 333f321 commit f4752d6

6 files changed

Lines changed: 34 additions & 32 deletions

File tree

config/services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ services:
4040
phpbb.wpn.ucp.controller.webpush:
4141
class: phpbb\webpushnotifications\ucp\controller\webpush
4242
arguments:
43+
- '@config'
4344
- '@controller.helper'
4445
- '@dbal.conn'
4546
- '@phpbb.wpn.form_helper'

notification/method/webpush.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ protected function notify_using_webpush(): void
221221
$data = [
222222
'item_id' => $notification->item_id,
223223
'type_id' => $notification->notification_type_id,
224+
'version' => $this->config['assets_version'],
224225
];
225226
$json_data = json_encode($data);
226227

styles/all/template/push_worker.js.twig

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Event listener for install event
3+
*/
4+
self.addEventListener('install', () => {
5+
// Call to ensure service worker is correctly updated
6+
self.skipWaiting();
7+
});
8+
9+
/**
10+
* Event listener for activate event
11+
*/
12+
self.addEventListener('activate', event => {
13+
event.waitUntil(self.clients.claim());
14+
});
15+
116
/**
217
* Event listener for push event
318
*/
@@ -7,17 +22,25 @@ self.addEventListener('push', event => {
722
}
823

924
let itemId = 0;
10-
let typeId = 0;
25+
let typeId = 0;
26+
let notificationVersion = 5;
1127
try {
1228
const notificationData = event.data.json();
1329
itemId = notificationData.item_id;
1430
typeId = notificationData.type_id;
31+
notificationVersion = parseInt(notificationData.version, 10);
1532
} catch {
1633
self.registration.showNotification(event.data.text());
1734
return;
1835
}
1936

2037
const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}';
38+
const assetsVersion = parseInt('{{ ASSETS_VERSION }}', 10);
39+
40+
// Force update if versions differ
41+
if (assetsVersion !== notificationVersion) {
42+
self.registration.update();
43+
}
2144

2245
const formData = new FormData();
2346
formData.append('item_id', itemId.toString(10));

styles/all/template/ucp_notifications_webpush.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@
1818
</script>
1919

2020
{% INCLUDEJS '@phpbb_webpushnotifications/webpush.js' %}
21-
{% INCLUDEJS '@phpbb_webpushnotifications/update_worker.js' %}
2221
{% INCLUDECSS '@phpbb_webpushnotifications/phpbb_wpn.css' %}

styles/all/template/update_worker.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

ucp/controller/webpush.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace phpbb\webpushnotifications\ucp\controller;
1212

13+
use phpbb\config\config;
1314
use phpbb\controller\helper as controller_helper;
1415
use phpbb\db\driver\driver_interface;
1516
use phpbb\exception\http_exception;
@@ -31,6 +32,9 @@ class webpush
3132
/** @var string UCP form token name */
3233
public const FORM_TOKEN_UCP = 'ucp_webpush';
3334

35+
/** @var config */
36+
protected $config;
37+
3438
/** @var controller_helper */
3539
protected $controller_helper;
3640

@@ -61,6 +65,7 @@ class webpush
6165
/**
6266
* Constructor for webpush controller
6367
*
68+
* @param config $config
6469
* @param controller_helper $controller_helper
6570
* @param driver_interface $db
6671
* @param form_helper $form_helper
@@ -71,9 +76,10 @@ class webpush
7176
* @param string $notification_webpush_table
7277
* @param string $push_subscriptions_table
7378
*/
74-
public function __construct(controller_helper $controller_helper, driver_interface $db, form_helper $form_helper, path_helper $path_helper,
79+
public function __construct(config $config, controller_helper $controller_helper, driver_interface $db, form_helper $form_helper, path_helper $path_helper,
7580
request_interface $request, user $user, Environment $template, string $notification_webpush_table, string $push_subscriptions_table)
7681
{
82+
$this->config = $config;
7783
$this->controller_helper = $controller_helper;
7884
$this->db = $db;
7985
$this->form_helper = $form_helper;
@@ -129,6 +135,7 @@ public function worker(): Response
129135
// @todo: only work for logged in users, no anonymous & bot
130136
$content = $this->template->render('@phpbb_webpushnotifications/push_worker.js.twig', [
131137
'U_WEBPUSH_GET_NOTIFICATION' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_get_notification_controller'),
138+
'ASSETS_VERSION' => $this->config['assets_version'],
132139
]);
133140

134141
$response = new Response($content);

0 commit comments

Comments
 (0)