Skip to content

Commit a8ff043

Browse files
committed
Show prompt based on device and service worker being subbed
1 parent 7f17201 commit a8ff043

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

notification/method/webpush.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public function get_ucp_template_data(helper $controller_helper, form_helper $fo
401401
'U_WEBPUSH_WORKER_URL' => $controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
402402
'SUBSCRIPTIONS' => $subscriptions,
403403
'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,
404+
'S_WEBPUSH_POPUP_PROMPT' => $this->config['wpn_webpush_popup_prompt'] && !$this->user->data['user_wpn_popup_declined'] && $this->user->id() != ANONYMOUS && $this->user->data['user_type'] != USER_IGNORE,
405405
];
406406
}
407407

styles/all/template/webpush.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,27 @@ function PhpbbWebpush() {
130130
return;
131131
}
132132

133+
// Check if this browser already has a subscription
134+
navigator.serviceWorker.getRegistration(serviceWorkerUrl)
135+
.then(registration => {
136+
if (typeof registration === 'undefined') {
137+
showPopup(popup);
138+
return;
139+
}
140+
141+
registration.pushManager.getSubscription()
142+
.then(subscription => {
143+
if (!isValidSubscription(subscription)) {
144+
showPopup(popup);
145+
}
146+
});
147+
});
148+
}
149+
150+
/**
151+
* Show popup with event handlers
152+
*/
153+
function showPopup(popup) {
133154
setTimeout(() => {
134155
popup.style.display = 'flex';
135156
}, 1000);

0 commit comments

Comments
 (0)