Notification improvements
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
cb00f6f6b0
commit
7cb40bd9e2
@ -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;
|
||||||
|
@ -331,6 +331,7 @@ export default class Notifications extends Vue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async subscribeToWebPush(): Promise<void> {
|
async subscribeToWebPush(): Promise<void> {
|
||||||
|
try {
|
||||||
if (this.canShowWebPush()) {
|
if (this.canShowWebPush()) {
|
||||||
const subscription = await subscribeUserToPush();
|
const subscription = await subscribeUserToPush();
|
||||||
if (subscription) {
|
if (subscription) {
|
||||||
@ -350,9 +351,13 @@ export default class Notifications extends Vue {
|
|||||||
} else {
|
} else {
|
||||||
console.log("can't do webpush");
|
console.log("can't do webpush");
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async unsubscribeToWebPush(): Promise<void> {
|
async unsubscribeToWebPush(): Promise<void> {
|
||||||
|
try {
|
||||||
const endpoint = await unsubscribeUserToPush();
|
const endpoint = await unsubscribeUserToPush();
|
||||||
if (endpoint) {
|
if (endpoint) {
|
||||||
const { data } = await this.$apollo.mutate({
|
const { data } = await this.$apollo.mutate({
|
||||||
@ -364,6 +369,9 @@ export default class Notifications extends Vue {
|
|||||||
console.log(data);
|
console.log(data);
|
||||||
this.subscribed = false;
|
this.subscribed = false;
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
canShowWebPush(): boolean {
|
canShowWebPush(): boolean {
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user