Skip to content

Commit dd91568

Browse files
committed
Add notification subscribe popup prompt
1 parent b06c7c7 commit dd91568

16 files changed

Lines changed: 355 additions & 0 deletions

acp/wpn_acp_module.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public function display_settings()
108108
'WEBPUSH_VAPID_PRIVATE' => $this->config['wpn_webpush_vapid_private'] ? self::MASKED_PRIVATE_KEY : '',
109109
'S_WEBPUSH_DROPDOWN_SUBSCRIBE' => $this->config['wpn_webpush_dropdown_subscribe'],
110110
'S_WEBPUSH_METHOD_ENABLED' => $this->config['wpn_webpush_method_enabled'],
111+
'S_WEBPUSH_POPUP_PROMPT' => $this->config['wpn_webpush_popup_prompt'],
111112
'U_ACTION' => $this->u_action,
112113
]);
113114

@@ -133,6 +134,7 @@ public function save_settings()
133134
'wpn_webpush_vapid_private'=> ['validate' => 'string:25:255', 'lang' => 'WEBPUSH_VAPID_PRIVATE'],
134135
'wpn_webpush_dropdown_subscribe' => ['validate' => 'bool'],
135136
'wpn_webpush_method_enabled' => ['validate' => 'bool'],
137+
'wpn_webpush_popup_prompt' => ['validate' => 'bool'],
136138
];
137139

138140
// Do not validate and update private key field if the content is ******** and the key was already set

adm/style/wpn_acp_settings.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ <h3>{{ lang('WARNING') }}</h3>
3939
<dd><label><input type="radio" class="radio" name="config[wpn_webpush_method_enabled]" value="1" {% if S_WEBPUSH_METHOD_ENABLED %}checked="checked"{% endif %}> {{ lang('YES') }}</label>
4040
<label><input type="radio" class="radio" name="config[wpn_webpush_method_enabled]" value="0"{% if not S_WEBPUSH_METHOD_ENABLED %} checked="checked"{% endif %}> {{ lang('NO') }}</label></dd>
4141
</dl>
42+
<dl>
43+
<dt><label>{{ lang('WEBPUSH_POPUP_PROMPT') ~ lang('COLON') }}</label><br><span>{{ lang('WEBPUSH_POPUP_PROMPT_EXPLAIN') }}</span></dt>
44+
<dd><label><input type="radio" class="radio" name="config[wpn_webpush_popup_prompt]" value="1" {% if S_WEBPUSH_POPUP_PROMPT %}checked="checked"{% endif %}> {{ lang('YES') }}</label>
45+
<label><input type="radio" class="radio" name="config[wpn_webpush_popup_prompt]" value="0"{% if not S_WEBPUSH_POPUP_PROMPT %} checked="checked"{% endif %}> {{ lang('NO') }}</label></dd>
46+
</dl>
4247
</fieldset>
4348
<fieldset class="submit-buttons">
4449
<input class="button1" type="submit" id="submit" name="submit" value="{{ lang('SUBMIT') }}">&nbsp;

config/wpn_ucp.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ phpbb_webpushnotifications_ucp_push_subscribe_controller:
1313
phpbb_webpushnotifications_ucp_push_unsubscribe_controller:
1414
path: /push/unsubscribe
1515
defaults: { _controller: phpbb.wpn.ucp.controller.webpush:unsubscribe }
16+
17+
phpbb_webpushnotifications_ucp_push_decline_popup_controller:
18+
path: /push/decline-popup
19+
defaults: { _controller: phpbb.wpn.ucp.controller.webpush:decline_popup }

language/en/webpushnotifications_module_acp.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,7 @@
5050
'WEBPUSH_METHOD_ENABLED_EXPLAIN'=> 'When this setting is enabled, users who have also enabled and allowed browser notifications will start receiving them automatically. They can visit the UCP Notification settings to disable any unwanted notifications.<br><br>If this setting is disabled, users will not receive any notifications, even if they have enabled push notifications, until they visit the UCP Notification settings to allow the specific notifications they wish to receive.',
5151
'WEBPUSH_DROPDOWN_SUBSCRIBE' => 'Show web push settings in the notification dropdown',
5252
'WEBPUSH_DROPDOWN_SUBSCRIBE_EXPLAIN'=> 'Show or hide the “Enable Web Push” toggle switch in the notification dropdown. This allows users to easily enable or disable push notifications from any page of the forum.',
53+
'WEBPUSH_POPUP_PROMPT' => 'Show popup prompt for unsubscribed members',
54+
'WEBPUSH_POPUP_PROMPT_EXPLAIN' => 'Display a popup message asking registered members if they want to receive push notifications. The popup will only appear to members who are not currently subscribed and have not previously declined.',
5355
'WEBPUSH_INSECURE_SERVER_ERROR' => 'This board is not using a secure SSL/HTTPS protocol, which is required for enabling web push notifications. Alternatively, the server environment might be misconfigured. Ensure that the <em>HTTPS</em> and <em>HEADER_CLIENT_PROTO</em> server environment variables are correctly configured.',
5456
]);

language/en/webpushnotifications_module_ucp.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,8 @@
4949
'NOTIFY_WEBPUSH_DROPDOWN_TITLE' => 'Visit notifications settings to set your preferred push notifications.',
5050
'NOTIFY_WEBPUSH_DENIED' => 'You have denied notifications from this site. To enable push notifications, allow notifications from this site in your browser settings.',
5151
'NOTIFY_WEBPUSH_DISABLED' => 'Push notifications not supported',
52+
'NOTIFY_WEBPUSH_POPUP_TITLE' => 'Enable Push Notifications?',
53+
'NOTIFY_WEBPUSH_POPUP_MESSAGE' => 'Stay updated with instant notifications for new replies, mentions, and more.',
54+
'NOTIFY_WEBPUSH_POPUP_ALLOW' => 'Allow',
55+
'NOTIFY_WEBPUSH_POPUP_DECLINE' => 'Decline',
5256
]);

migrations/add_popup_prompt.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
*
4+
* phpBB Browser Push Notifications. An extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2025, phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\webpushnotifications\migrations;
12+
13+
use phpbb\db\migration\migration;
14+
15+
class add_popup_prompt extends migration
16+
{
17+
public function effectively_installed(): bool
18+
{
19+
return $this->config->offsetExists('wpn_webpush_popup_prompt');
20+
}
21+
22+
public static function depends_on()
23+
{
24+
return ['\phpbb\webpushnotifications\migrations\add_acp_configs'];
25+
}
26+
27+
public function update_schema(): array
28+
{
29+
return [
30+
'add_columns' => [
31+
$this->table_prefix . 'users' => [
32+
'user_wpn_popup_declined' => ['BOOL', 0],
33+
],
34+
],
35+
];
36+
}
37+
38+
public function revert_schema(): array
39+
{
40+
return [
41+
'drop_columns' => [
42+
$this->table_prefix . 'users' => [
43+
'user_wpn_popup_declined',
44+
],
45+
],
46+
];
47+
}
48+
49+
public function update_data(): array
50+
{
51+
return [
52+
['config.add', ['wpn_webpush_popup_prompt', false]],
53+
];
54+
}
55+
56+
public function revert_data(): array
57+
{
58+
return [
59+
['config.remove', ['wpn_webpush_popup_prompt']],
60+
];
61+
}
62+
}

notification/method/webpush.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,12 @@ public function get_ucp_template_data(helper $controller_helper, form_helper $fo
396396
'NOTIFICATIONS_WEBPUSH_ENABLE' => ($this->config['load_notifications'] && $this->config['allow_board_notifications'] && $this->config['wpn_webpush_dropdown_subscribe']) || stripos($this->user->page['page'], 'notification_options'),
397397
'U_WEBPUSH_SUBSCRIBE' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_subscribe_controller'),
398398
'U_WEBPUSH_UNSUBSCRIBE' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_unsubscribe_controller'),
399+
'U_WEBPUSH_DECLINE_POPUP' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_decline_popup_controller'),
399400
'VAPID_PUBLIC_KEY' => $this->config['wpn_webpush_vapid_public'],
400401
'U_WEBPUSH_WORKER_URL' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
401402
'SUBSCRIPTIONS' => $subscriptions,
402403
'WEBPUSH_FORM_TOKENS' => $form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP),
404+
'S_WEBPUSH_POPUP_PROMPT' => $this->config['wpn_webpush_popup_prompt'] && empty($subscriptions) && !$this->user->data['user_wpn_popup_declined'] && $this->user->id() != ANONYMOUS && $this->user->data['user_type'] != USER_IGNORE,
403405
];
404406
}
405407

styles/all/template/event/overall_header_head_append.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77

88
{% if NOTIFICATIONS_WEBPUSH_ENABLE %}
99
{% include '@phpbb_webpushnotifications/ucp_notifications_webpush.html' %}
10+
{% include '@phpbb_webpushnotifications/webpush_popup.html' %}
1011
{% endif %}

styles/all/template/ucp_notifications_webpush.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
serviceWorkerUrl: '{{ U_WEBPUSH_WORKER_URL }}',
44
subscribeUrl: '{{ U_WEBPUSH_SUBSCRIBE }}',
55
unsubscribeUrl: '{{ U_WEBPUSH_UNSUBSCRIBE }}',
6+
declinePopupUrl: '{{ U_WEBPUSH_DECLINE_POPUP }}',
67
ajaxErrorTitle: '{{ lang('AJAX_ERROR_TITLE') }}',
78
vapidPublicKey: '{{ VAPID_PUBLIC_KEY }}',
89
formTokens: {

styles/all/template/webpush.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ function PhpbbWebpush() {
3333
/** @type {HTMLElement} Unsubscribe button */
3434
let unsubscribeButton;
3535

36+
/** @type {string} URL to decline popup */
37+
let declinePopupUrl = '';
38+
3639
/**
3740
* Init function for phpBB Web Push
3841
* @type {array} options
@@ -41,6 +44,7 @@ function PhpbbWebpush() {
4144
serviceWorkerUrl = options.serviceWorkerUrl;
4245
subscribeUrl = options.subscribeUrl;
4346
unsubscribeUrl = options.unsubscribeUrl;
47+
declinePopupUrl = options.declinePopupUrl;
4448
this.formTokens = options.formTokens;
4549
subscriptions = options.subscriptions;
4650
ajaxErrorTitle = options.ajaxErrorTitle;
@@ -62,6 +66,7 @@ function PhpbbWebpush() {
6266
unsubscribeButton.addEventListener('click', unsubscribeButtonHandler);
6367

6468
updateButtonState();
69+
initPopupPrompt();
6570
})
6671
.catch(error => {
6772
console.info(error);
@@ -116,6 +121,50 @@ function PhpbbWebpush() {
116121
}
117122
}
118123

124+
/**
125+
* Initialize popup prompt
126+
*/
127+
function initPopupPrompt() {
128+
const popup = document.getElementById('wpn_popup_prompt');
129+
if (!popup || Notification.permission === 'denied') {
130+
return;
131+
}
132+
133+
setTimeout(() => {
134+
popup.style.display = 'flex';
135+
}, 1000);
136+
137+
const allowBtn = document.getElementById('wpn_popup_allow');
138+
const declineBtn = document.getElementById('wpn_popup_decline');
139+
140+
if (allowBtn) {
141+
allowBtn.addEventListener('click', () => {
142+
popup.style.display = 'none';
143+
subscribeButtonHandler({ preventDefault: () => {} });
144+
});
145+
}
146+
147+
if (declineBtn) {
148+
declineBtn.addEventListener('click', () => {
149+
popup.style.display = 'none';
150+
declinePopup();
151+
});
152+
}
153+
}
154+
155+
/**
156+
* Send decline popup request
157+
*/
158+
function declinePopup() {
159+
fetch(declinePopupUrl, {
160+
method: 'POST',
161+
headers: {
162+
'X-Requested-With': 'XMLHttpRequest',
163+
},
164+
body: getFormData({}),
165+
}).catch(error => console.info(error));
166+
}
167+
119168
/**
120169
* Check whether subscription is valid
121170
*
@@ -258,6 +307,10 @@ function PhpbbWebpush() {
258307
if ('form_tokens' in response) {
259308
updateFormTokens(response.form_tokens);
260309
}
310+
const popup = document.getElementById('wpn_popup_prompt');
311+
if (popup) {
312+
popup.style.display = 'none';
313+
}
261314
}
262315
}
263316

0 commit comments

Comments
 (0)