Allow setting up push when service worker hasn't been registered yet.

We don't register the service worker until the user opts in for push notifications.
This commit is contained in:
Karina Kwiatek 2023-01-02 13:29:49 +01:00 committed by Andreas Nedbal
parent 7ffd07a959
commit 1a29a19e9d
1 changed files with 18 additions and 15 deletions

View File

@ -12,24 +12,27 @@ export default (): void => {
if (swCapable && notificationCapable) {
const enableBtn = document.querySelector('button[data-action="push-enable"]');
navigator.serviceWorker.getRegistration().then(registration =>
registration?.pushManager.getSubscription().then(subscription => {
if (subscription) {
document.querySelector('button[data-action="push-enable"]')?.classList.add('d-none');
document.querySelector('[data-action="push-disable"]')?.classList.remove('d-none');
if (!subscriptionChecked) {
checkSubscription(subscription);
subscriptionChecked = true;
}
} else {
enableBtn?.classList.remove('d-none');
navigator.serviceWorker.getRegistration().then(async registration => {
const subscription = await registration?.pushManager.getSubscription();
if (subscription) {
document.querySelector('button[data-action="push-enable"]')?.classList.add('d-none');
document.querySelector('[data-action="push-disable"]')?.classList.remove('d-none');
if (localStorage.getItem('dismiss-push-settings-prompt') == null) {
document.querySelector('.push-settings')?.classList.remove('d-none');
}
if (!subscriptionChecked) {
checkSubscription(subscription);
subscriptionChecked = true;
}
}));
}
if (!registration) {
enableBtn?.classList.remove('d-none');
if (localStorage.getItem('dismiss-push-settings-prompt') == null) {
document.querySelector('.push-settings')?.classList.remove('d-none');
}
}
});
}
registerEvents([