Notification improvements

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-05-12 18:30:02 +02:00
parent cb00f6f6b0
commit 7cb40bd9e2
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
2 changed files with 36 additions and 32 deletions

View file

@ -44,11 +44,6 @@ export async function subscribeUserToPush(): Promise<PushSubscription | null> {
return null; return null;
} }
export async function isSubscribed(): Promise<boolean> {
const registration = await navigator.serviceWorker.ready;
return (await registration.pushManager.getSubscription()) !== null;
}
export async function unsubscribeUserToPush(): Promise<string | undefined> { export async function unsubscribeUserToPush(): Promise<string | undefined> {
console.log("performing unsubscribeUserToPush"); console.log("performing unsubscribeUserToPush");
const registration = await navigator.serviceWorker.ready; const registration = await navigator.serviceWorker.ready;

View file

@ -331,38 +331,46 @@ export default class Notifications extends Vue {
} }
async subscribeToWebPush(): Promise<void> { async subscribeToWebPush(): Promise<void> {
if (this.canShowWebPush()) { try {
const subscription = await subscribeUserToPush(); if (this.canShowWebPush()) {
if (subscription) { const subscription = await subscribeUserToPush();
const subscriptionJSON = subscription?.toJSON(); if (subscription) {
console.log("subscription", subscriptionJSON); const subscriptionJSON = subscription?.toJSON();
const { data } = await this.$apollo.mutate({ console.log("subscription", subscriptionJSON);
mutation: REGISTER_PUSH_MUTATION, const { data } = await this.$apollo.mutate({
variables: { mutation: REGISTER_PUSH_MUTATION,
endpoint: subscriptionJSON.endpoint, variables: {
auth: subscriptionJSON?.keys?.auth, endpoint: subscriptionJSON.endpoint,
p256dh: subscriptionJSON?.keys?.p256dh, auth: subscriptionJSON?.keys?.auth,
}, p256dh: subscriptionJSON?.keys?.p256dh,
}); },
this.subscribed = true; });
console.log(data); this.subscribed = true;
console.log(data);
}
} else {
console.log("can't do webpush");
} }
} else { } catch (e) {
console.log("can't do webpush"); console.error(e);
} }
} }
async unsubscribeToWebPush(): Promise<void> { async unsubscribeToWebPush(): Promise<void> {
const endpoint = await unsubscribeUserToPush(); try {
if (endpoint) { const endpoint = await unsubscribeUserToPush();
const { data } = await this.$apollo.mutate({ if (endpoint) {
mutation: UNREGISTER_PUSH_MUTATION, const { data } = await this.$apollo.mutate({
variables: { mutation: UNREGISTER_PUSH_MUTATION,
endpoint, variables: {
}, endpoint,
}); },
console.log(data); });
this.subscribed = false; 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<boolean> { private async isSubscribed(): Promise<boolean> {
if (!("serviceWorker" in navigator)) return Promise.resolve(false);
const registration = await navigator.serviceWorker.getRegistration(); const registration = await navigator.serviceWorker.getRegistration();
return (await registration?.pushManager.getSubscription()) !== null; return (await registration?.pushManager.getSubscription()) !== null;
} }