diff --git a/js/src/services/push-subscription.ts b/js/src/services/push-subscription.ts index d94a95c8c..841ad5df6 100644 --- a/js/src/services/push-subscription.ts +++ b/js/src/services/push-subscription.ts @@ -44,11 +44,6 @@ export async function subscribeUserToPush(): Promise { return null; } -export async function isSubscribed(): Promise { - const registration = await navigator.serviceWorker.ready; - return (await registration.pushManager.getSubscription()) !== null; -} - export async function unsubscribeUserToPush(): Promise { console.log("performing unsubscribeUserToPush"); const registration = await navigator.serviceWorker.ready; diff --git a/js/src/views/Settings/Notifications.vue b/js/src/views/Settings/Notifications.vue index 93dfa7017..e4e3a998c 100644 --- a/js/src/views/Settings/Notifications.vue +++ b/js/src/views/Settings/Notifications.vue @@ -331,38 +331,46 @@ export default class Notifications extends Vue { } async subscribeToWebPush(): Promise { - if (this.canShowWebPush()) { - const subscription = await subscribeUserToPush(); - if (subscription) { - const subscriptionJSON = subscription?.toJSON(); - console.log("subscription", subscriptionJSON); - const { data } = await this.$apollo.mutate({ - mutation: REGISTER_PUSH_MUTATION, - variables: { - endpoint: subscriptionJSON.endpoint, - auth: subscriptionJSON?.keys?.auth, - p256dh: subscriptionJSON?.keys?.p256dh, - }, - }); - this.subscribed = true; - console.log(data); + try { + if (this.canShowWebPush()) { + const subscription = await subscribeUserToPush(); + if (subscription) { + const subscriptionJSON = subscription?.toJSON(); + console.log("subscription", subscriptionJSON); + const { data } = await this.$apollo.mutate({ + mutation: REGISTER_PUSH_MUTATION, + variables: { + endpoint: subscriptionJSON.endpoint, + auth: subscriptionJSON?.keys?.auth, + p256dh: subscriptionJSON?.keys?.p256dh, + }, + }); + this.subscribed = true; + console.log(data); + } + } else { + console.log("can't do webpush"); } - } else { - console.log("can't do webpush"); + } catch (e) { + console.error(e); } } async unsubscribeToWebPush(): Promise { - const endpoint = await unsubscribeUserToPush(); - if (endpoint) { - const { data } = await this.$apollo.mutate({ - mutation: UNREGISTER_PUSH_MUTATION, - variables: { - endpoint, - }, - }); - console.log(data); - this.subscribed = false; + try { + const endpoint = await unsubscribeUserToPush(); + if (endpoint) { + const { data } = await this.$apollo.mutate({ + mutation: UNREGISTER_PUSH_MUTATION, + variables: { + endpoint, + }, + }); + console.log(data); + this.subscribed = false; + } + } catch (e) { + console.error(e); } } @@ -375,6 +383,7 @@ export default class Notifications extends Vue { } private async isSubscribed(): Promise { + if (!("serviceWorker" in navigator)) return Promise.resolve(false); const registration = await navigator.serviceWorker.getRegistration(); return (await registration?.pushManager.getSubscription()) !== null; }