Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion language/en/webpushnotifications_module_ucp.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
'NOTIFY_WEBPUSH_UNSUBSCRIBE' => 'Disable to unsubscribe',
'NOTIFY_WEBPUSH_DROPDOWN_TITLE' => 'Visit notifications settings to set your preferred push notifications.',
'NOTIFY_WEBPUSH_DENIED' => 'You have denied notifications from this site. To enable push notifications, allow notifications from this site in your browser settings.',
'NOTIFY_WEBPUSH_DISABLED' => 'Push notifications not supported',
'NOTIFY_WEBPUSH_NOT_SUPPORTED' => 'Push notifications not supported',
'NOTIFY_WEBPUSH_POPUP_TITLE' => 'Allow browser notifications?',
'NOTIFY_WEBPUSH_POPUP_MESSAGE' => 'We would like to send you browser notifications for replies, private messages, and relevant forum activity. Optional — you can manage these settings at any time.',
'NOTIFY_WEBPUSH_POPUP_ALLOW' => 'Allow',
Expand Down
2 changes: 1 addition & 1 deletion language/ru/webpushnotifications_module_ucp.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
'NOTIFY_WEBPUSH_UNSUBSCRIBE' => 'Отписаться',
'NOTIFY_WEBPUSH_DROPDOWN_TITLE' => 'Посетите настройки уведомлений, чтобы установить предпочтительные типы браузерных уведомлений.',
'NOTIFY_WEBPUSH_DENIED' => 'Вы запретили браузерные уведомления для даного сайта. Для того, чтобы подписаться, необходимо их разрешить в настройках браузера.',
'NOTIFY_WEBPUSH_DISABLED' => 'Не поддерживается',
'NOTIFY_WEBPUSH_NOT_SUPPORTED' => 'Не поддерживается',
'NOTIFY_WEBPUSH_POPUP_TITLE' => 'Включить браузерные уведомления?',
'NOTIFY_WEBPUSH_POPUP_MESSAGE' => 'Браузерные уведомления позволяют быстро получать информацию о новых ответах, личных сообщениях и других активностях на данной конференции. Функцию можно отключить или включить в любое время в настройках уведомлений в Личном разделе.',
'NOTIFY_WEBPUSH_POPUP_ALLOW' => 'Включить',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% if NOTIFICATIONS_WEBPUSH_ENABLE and notification_types is not defined %}
<div class="wpn-notification-dropdown-footer" title="{{ lang('NOTIFY_WEBPUSH_DROPDOWN_TITLE') }}">
<span class="ellipsis-text">{{ lang('NOTIFY_WEBPUSH_ENABLE_SLIDER') ~ lang('COLON') }}</span>
<button id="subscribe_webpush" name="subscribe_webpush" data-l-err="{{ lang('INFORMATION') }}" data-l-msg="{{ lang('NOTIFY_WEBPUSH_DENIED') }}">
<button id="subscribe_webpush" name="subscribe_webpush" data-l-err="{{ lang('INFORMATION')|e('html_attr') }}" data-l-msg="{{ lang('NOTIFY_WEBPUSH_DENIED')|e('html_attr') }}" data-l-unsupported="{{ lang('NOTIFY_WEBPUSH_NOT_SUPPORTED')|e('html_attr') }}">
<i class="icon fa-toggle-off fa-fw icon-lightgray" aria-hidden="true" title="{{ lang('NOTIFY_WEBPUSH_SUBSCRIBE') }}"><span class="sr-only">{{ lang('NOTIFY_WEBPUSH_SUBSCRIBE') }}</span></i>
</button>
<button id="unsubscribe_webpush" name="unsubscribe_webpush" class="hidden">
Expand Down
2 changes: 1 addition & 1 deletion styles/all/template/ucp_notifications_webpush.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
serviceWorkerUrl: '{{ U_WEBPUSH_WORKER_URL }}',
subscribeUrl: '{{ U_WEBPUSH_SUBSCRIBE }}',
unsubscribeUrl: '{{ U_WEBPUSH_UNSUBSCRIBE }}',
ajaxErrorTitle: '{{ lang('AJAX_ERROR_TITLE') }}',
ajaxErrorTitle: '{{ lang_js('AJAX_ERROR_TITLE') }}',
vapidPublicKey: '{{ VAPID_PUBLIC_KEY }}',
formTokens: {
creationTime: '{{ WEBPUSH_FORM_TOKENS.creation_time }}',
Expand Down
88 changes: 50 additions & 38 deletions styles/all/template/webpush.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function PhpbbWebpush() {
}

if (subscribeButton.type === 'submit' || subscribeButton.classList.contains('button')) {
subscribeButton.value = subscribeButton.getAttribute('data-disabled-msg');
subscribeButton.value = subscribeButton.getAttribute('data-l-unsupported');
}
}

Expand Down Expand Up @@ -164,8 +164,8 @@ function PhpbbWebpush() {
allowBtn.addEventListener('click', (event) => {
event.stopPropagation();
popup.style.display = 'none';
subscribeButtonHandler({
preventDefault: () => {}
subscribeButtonHandler(event).catch(error => {
console.error('Subscription handler error:', error);
});
});
}
Expand Down Expand Up @@ -237,48 +237,60 @@ function PhpbbWebpush() {
async function subscribeButtonHandler(event) {
event.preventDefault();

subscribeButton.addEventListener('click', subscribeButtonHandler);
subscribeButton.removeEventListener('click', subscribeButtonHandler);

// Prevent the user from clicking the subscribe button multiple times.
const result = await Notification.requestPermission();
if (result === 'denied') {
phpbb.alert(subscribeButton.getAttribute('data-l-err'), subscribeButton.getAttribute('data-l-msg'));
return;
}
try {
// Prevent the user from clicking the subscribe button multiple times.
const result = await Notification.requestPermission();
if (result === 'denied') {
phpbb.alert(subscribeButton.getAttribute('data-l-err'), subscribeButton.getAttribute('data-l-msg'));
return;
}

const registration = await navigator.serviceWorker.getRegistration(serviceWorkerUrl);
const registration = await navigator.serviceWorker.getRegistration(serviceWorkerUrl);

// We might already have a subscription that is unknown to this instance of phpBB.
// Unsubscribe before trying to subscribe again.
if (typeof registration !== 'undefined') {
const subscribed = await registration.pushManager.getSubscription();
if (subscribed) {
await subscribed.unsubscribe();
// We might already have a subscription that is unknown to this instance of phpBB.
// Unsubscribe before trying to subscribe again.
if (typeof registration !== 'undefined') {
const subscribed = await registration.pushManager.getSubscription();
if (subscribed) {
await subscribed.unsubscribe();
}
}
}

const newSubscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array(vapidPublicKey),
});
const newSubscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array(vapidPublicKey),
});

const loadingIndicator = phpbb.loadingIndicator();
fetch(subscribeUrl, {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
body: getFormData(newSubscription),
})
.then(response => {
loadingIndicator.fadeOut(phpbb.alertTime);
return response.json();
const loadingIndicator = phpbb.loadingIndicator();
fetch(subscribeUrl, {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
body: getFormData(newSubscription),
})
.then(handleSubscribe)
.catch(error => {
loadingIndicator.fadeOut(phpbb.alertTime);
phpbb.alert(ajaxErrorTitle, error);
});
.then(response => {
loadingIndicator.fadeOut(phpbb.alertTime);
return response.json();
})
.then(handleSubscribe)
.catch(error => {
loadingIndicator.fadeOut(phpbb.alertTime);
phpbb.alert(ajaxErrorTitle, error);
});
} catch (error) {
promptDenied.set(); // deny the prompt on error to prevent repeated prompting
const popup = document.getElementById('wpn_popup_prompt');
if (popup) {
popup.style.display = 'none';
}
console.error('Push subscription error:', error);
phpbb.alert(subscribeButton.getAttribute('data-l-err'), error.message || subscribeButton.getAttribute('data-l-unsupported'));
} finally {
subscribeButton.addEventListener('click', subscribeButtonHandler);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<dl>
<dt><label for="subscribe_webpush">{{ lang('NOTIFY_WEBPUSH_NOTIFICATIONS') ~ lang('COLON') }}</label></dt>
<dd>
<input id="subscribe_webpush" type="submit" name="subscribe_webpush" value="{{ lang('NOTIFY_WEBPUSH_ENABLE') }}" class="wpn button1 button button-form" data-l-err="{{ lang('INFORMATION') }}" data-l-msg="{{ lang('NOTIFY_WEBPUSH_DENIED') }}" data-disabled-msg="{{ lang('NOTIFY_WEBPUSH_DISABLED') }}">
<input id="subscribe_webpush" type="submit" name="subscribe_webpush" value="{{ lang('NOTIFY_WEBPUSH_ENABLE') }}" class="wpn button1 button button-form" data-l-err="{{ lang('INFORMATION')|e('html_attr') }}" data-l-msg="{{ lang('NOTIFY_WEBPUSH_DENIED')|e('html_attr') }}" data-l-unsupported="{{ lang('NOTIFY_WEBPUSH_NOT_SUPPORTED')|e('html_attr') }}">
<input id="unsubscribe_webpush" type="submit" name="unsubscribe_webpush" value="{{ lang('NOTIFY_WEBPUSH_DISABLE') }}" class="wpn button1 button button-form hidden">
<br><span>{{ lang('NOTIFY_WEBPUSH_ENABLE_EXPLAIN') }}</span>
</dd>
Expand Down