Merge pull request #1370 from Retrospring/bugfix/webpush-registration-catch
Rewrite WebPush enable handler to properly catch errors
This commit is contained in:
commit
7eee7d38c6
|
@ -3,25 +3,26 @@ import I18n from "retrospring/i18n";
|
||||||
import { showNotification } from "utilities/notifications";
|
import { showNotification } from "utilities/notifications";
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
|
|
||||||
export function enableHandler (event: Event): void {
|
export async function enableHandler (event: Event): Promise<void> {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const sender = event.target as HTMLButtonElement;
|
const sender = event.target as HTMLButtonElement;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
getServiceWorker()
|
const registration = await getServiceWorker();
|
||||||
.then(subscribe)
|
const subscription = await subscribe(registration);
|
||||||
.then(async subscription => {
|
const permission = await Notification.requestPermission();
|
||||||
return Notification.requestPermission().then(permission => {
|
|
||||||
if (permission != "granted") {
|
if (permission != "granted") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
post('/ajax/webpush', {
|
const response = await post('/ajax/webpush', {
|
||||||
body: {
|
body: {
|
||||||
subscription
|
subscription
|
||||||
},
|
},
|
||||||
contentType: 'application/json'
|
contentType: 'application/json'
|
||||||
}).then(async response => {
|
});
|
||||||
|
|
||||||
const data = await response.json;
|
const data = await response.json;
|
||||||
|
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
|
@ -36,18 +37,18 @@ export function enableHandler (event: Event): void {
|
||||||
document.querySelector<HTMLDivElement>('.push-settings')?.classList.add('d-none');
|
document.querySelector<HTMLDivElement>('.push-settings')?.classList.add('d-none');
|
||||||
localStorage.setItem('dismiss-push-settings-prompt', 'true');
|
localStorage.setItem('dismiss-push-settings-prompt', 'true');
|
||||||
|
|
||||||
document.getElementById('subscription-count').textContent = data.message;
|
const subscriptionCountElement = document.getElementById('subscription-count');
|
||||||
|
if (subscriptionCountElement != null) {
|
||||||
|
subscriptionCountElement.textContent = data.message;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
new Notification(I18n.translate("frontend.push_notifications.fail.title"), {
|
new Notification(I18n.translate("frontend.push_notifications.fail.title"), {
|
||||||
body: I18n.translate("frontend.push_notifications.fail.body")
|
body: I18n.translate("frontend.push_notifications.fail.body")
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to set up push notifications", error);
|
console.error("Failed to set up push notifications", error);
|
||||||
showNotification(I18n.translate("frontend.push_notifications.setup_fail"));
|
showNotification(I18n.translate("frontend.push_notifications.setup_fail"), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue