mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-10 13:28:55 +00:00
[ticket/17010] Add first version of push worker javascript
PHPBB3-17010
This commit is contained in:
parent
06458c95f5
commit
e177ee3750
1 changed files with 59 additions and 0 deletions
59
phpBB/styles/all/js/push_worker.js.twig
Normal file
59
phpBB/styles/all/js/push_worker.js.twig
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
/**
|
||||||
|
* Event listener for push event
|
||||||
|
*/
|
||||||
|
self.addEventListener('push', event => {
|
||||||
|
if (typeof event.data === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let itemId = 0,
|
||||||
|
typeId = 0;
|
||||||
|
try {
|
||||||
|
const notificationData = event.data.json();
|
||||||
|
itemId = notificationData['item_id'];
|
||||||
|
typeId = notificationData['type_id'];
|
||||||
|
} catch (e) {
|
||||||
|
self.registration.showNotification(event.data.text());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}';
|
||||||
|
|
||||||
|
let formData = new FormData();
|
||||||
|
formData.append('item_id', itemId.toString(10));
|
||||||
|
formData.append('type_id', typeId.toString(10));
|
||||||
|
|
||||||
|
fetch(getNotificationUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'X-Requested-With': 'XMLHttpRequest'
|
||||||
|
},
|
||||||
|
body: formData
|
||||||
|
})
|
||||||
|
.then((response) => response.json())
|
||||||
|
.then((response) => {
|
||||||
|
const responseBody = response.title + "\n" + response.text;
|
||||||
|
const options = {
|
||||||
|
body: responseBody,
|
||||||
|
data: response,
|
||||||
|
icon: response.avatar.src
|
||||||
|
// foo: ''
|
||||||
|
//icon: image
|
||||||
|
}
|
||||||
|
self.registration.showNotification(
|
||||||
|
response.heading,
|
||||||
|
options
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event listener for notification click
|
||||||
|
*/
|
||||||
|
self.addEventListener('notificationclick', event => {
|
||||||
|
event.notification.close();
|
||||||
|
if (typeof event.notification.data !== 'undefined') {
|
||||||
|
event.waitUntil(self.clients.openWindow(event.notification.data.url));
|
||||||
|
}
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue