1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-05 00:07:44 +02:00

Merge pull request #6416 from marc1706/ticket/17010

[ticket/17010] Implement notification method for web push
This commit is contained in:
Marc Alexander
2024-04-03 20:05:23 +02:00
committed by GitHub
39 changed files with 5512 additions and 253 deletions

View File

@@ -0,0 +1,53 @@
/**
* Event listener for push event
*/
self.addEventListener('push', event => {
if (typeof event.data === 'undefined') {
return;
}
let itemId = 0;
let typeId = 0;
try {
const notificationData = event.data.json();
itemId = notificationData.item_id;
typeId = notificationData.type_id;
} catch {
self.registration.showNotification(event.data.text());
return;
}
const getNotificationUrl = '{{ U_WEBPUSH_GET_NOTIFICATION }}';
const 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,
};
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));
}
});

View File

@@ -12,38 +12,6 @@
<div class="inner">
<p class="cp-desc">{TITLE_EXPLAIN}</p>
<!-- IF MODE == 'notification_options' -->
<table class="table1">
<thead>
<tr>
<th>{L_NOTIFICATION_TYPE}</th>
<!-- BEGIN notification_methods -->
<th class="mark">{notification_methods.NAME}</th>
<!-- END notification_methods -->
</tr>
</thead>
<tbody>
<!-- BEGIN notification_types -->
<!-- IF notification_types.GROUP_NAME -->
<tr class="bg3">
<td colspan="{NOTIFICATION_TYPES_COLS}">{notification_types.GROUP_NAME}</td>
</tr>
<!-- ELSE -->
<tr class="<!-- IF notification_types.S_ROW_COUNT is odd -->bg1<!-- ELSE -->bg2<!-- ENDIF -->">
<td>
{notification_types.NAME}
<!-- IF notification_types.EXPLAIN --><br />&nbsp; &nbsp;{notification_types.EXPLAIN}<!-- ENDIF -->
</td>
<!-- BEGIN notification_methods -->
<td class="mark"><input type="checkbox" name="{notification_types.TYPE}_{notification_types.notification_methods.METHOD}"<!-- IF notification_types.notification_methods.SUBSCRIBED --> checked="checked"<!-- ENDIF --><!-- IF not notification_types.notification_methods.AVAILABLE --> disabled="disabled"<!-- ENDIF --> /></td>
<!-- END notification_methods -->
</tr>
<!-- ENDIF -->
<!-- END notification_types -->
</tbody>
</table>
<!-- ELSE -->
<!-- IF .notification_list -->
<div class="action-bar bar-top">
<div class="pagination">
@@ -105,8 +73,6 @@
<!-- ELSE -->
<p><strong>{L_NO_NOTIFICATIONS}</strong></p>
<!-- ENDIF -->
<!-- ENDIF -->
</div>
</div>

View File

@@ -0,0 +1,76 @@
{% include('ucp_header.html') %}
{% if NOTIFICATIONS_WEBPUSH_ENABLE %}
{% include('ucp_notifications_webpush.html') %}
{% endif %}
<form id="ucp" method="post" action="{{ S_UCP_ACTION }}"{{ S_FORM_ENCTYPE }}>
<h2 class="cp-title">{{ TITLE }}</h2>
{% if NOTIFICATIONS_WEBPUSH_ENABLE %}
<div class="panel">
<div class="inner">
<fieldset>
<dl>
<dt><label for="subscribe_webpush">{{ lang('NOTIFY_WEBPUSH_ENABLE') ~ lang('COLON') }}</label><br><span>{{ lang('NOTIFY_WEBPUSH_ENABLE_EXPLAIN') }}</span></dt>
<dd>
<input id="subscribe_webpush" type="submit" name="subscribe_webpush" value="{{ lang('NOTIFY_WEBPUSH_SUBSCRIBE') }}" class="button1 button button-form">
<input id="unsubscribe_webpush" type="submit" name="unsubscribe_webpush" value="{{ lang('NOTIFY_WEBPUSH_UNSUBSCRIBE') }}" class="button1 button button-form hidden">
</dd>
</dl>
</fieldset>
</div>
</div>
{% endif %}
<div class="panel">
<div class="inner">
<p class="cp-desc">{{ TITLE_EXPLAIN }}</p>
<table class="table1">
<thead>
<tr>
<th>{{ lang('NOTIFICATION_TYPE') }}</th>
{% for method in notification_methods %}
<th class="mark">{{ method.NAME }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for notification_type in notification_types %}
{% if notification_type.GROUP_NAME %}
<tr class="bg3">
<td colspan="{{ NOTIFICATION_TYPES_COLS }}">{{ notification_type.GROUP_NAME }}</td>
</tr>
{% else %}
<tr class="{% if loop.index is even %}bg1{% else %}bg2{% endif %}">
<td>
{{ notification_type.NAME }}
{% if notification_type.EXPLAIN %}<br>&nbsp; &nbsp;{{ notification_type.EXPLAIN }}{% endif %}
</td>
{% for notification_method in notification_type.notification_methods %}
{% apply spaceless %}
<td class="mark">
<input type="checkbox" name="{{ notification_type.TYPE }}_{{ notification_method.METHOD }}"{% if notification_method.SUBSCRIBED %} checked="checked"{% endif %}{% if not notification_method.AVAILABLE %} disabled="disabled"{% endif %}/></td>
{% endapply %}
{% endfor %}
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
</div>
{% if notification_types or notification_list %}
<fieldset class="display-actions">
<input type="hidden" name="form_time" value="{{ FORM_TIME }}" />
{{ S_HIDDEN_FIELDS }}
<input type="submit" name="submit" value="{% if MODE == 'notification_options' %}{{ lang('SUBMIT') }}{% else %}{{ lang('MARK_READ') }}{% endif %}" class="button1 button button-form" />
<div><a href="#" onclick="$('#ucp input:checkbox').prop('checked', true); return false;">{{ lang('MARK_ALL') }}</a> &bull; <a href="#" onclick="$('#ucp input:checkbox').prop('checked', false); return false;">{{ lang('UNMARK_ALL') }}</a></div>
{{ S_FORM_TOKEN }}
</fieldset>
{% endif %}
</form>
{% include('ucp_footer.html') %}

View File

@@ -0,0 +1,21 @@
<script>
phpbbWebpushOptions = {
serviceWorkerUrl: '{{ U_WEBPUSH_WORKER_URL }}',
subscribeUrl: '{{ U_WEBPUSH_SUBSCRIBE }}',
unsubscribeUrl: '{{ U_WEBPUSH_UNSUBSCRIBE }}',
ajaxErrorTitle: '{{ lang('AJAX_ERROR_TITLE') }}',
vapidPublicKey: '{{ VAPID_PUBLIC_KEY }}',
formTokens: {
creationTime: '{{ WEBPUSH_FORM_TOKENS.creation_time }}',
formToken: '{{ WEBPUSH_FORM_TOKENS.form_token }}',
},
subscriptions: [
{% for sub in SUBSCRIPTIONS %}
{endpoint: '{{ sub.endpoint }}', expiration: '{{ sub.expiration }}' },
{% endfor %}
],
}
</script>
{% INCLUDEJS(T_ASSETS_PATH ~ '/javascript/webpush.js') %}

View File

@@ -32,6 +32,20 @@
outline: none;
}
.button[disabled],
.button[disabled]:hover,
.button.disabled,
.button.disabled:hover {
background: #eeeeee;
border-color: #aaaaaa;
color: #aaaaaa;
cursor: default;
}
.button.hidden {
display: none;
}
.caret {
border-left: 1px solid;
position: relative;