|
@ -326,62 +326,51 @@ export default class App extends Vue { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private askPermission(): Promise<NotificationPermission> { |
|
|
private askPermission(): Promise<NotificationPermission> { |
|
|
if ("serviceWorker" in navigator && navigator.serviceWorker.controller) { |
|
|
if (!("serviceWorker" in navigator && navigator.serviceWorker.controller)) { |
|
|
|
|
|
return Promise.reject("Service worker not available."); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const secret = localStorage.getItem("secret"); |
|
|
const secret = localStorage.getItem("secret"); |
|
|
|
|
|
if (!secret) { |
|
|
|
|
|
return Promise.reject("No secret found."); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (secret) { |
|
|
return this.sendSecretToServiceWorker(secret) |
|
|
|
|
|
.then(() => this.checkNotificationSupport()) |
|
|
|
|
|
.then(() => this.requestNotificationPermission()) |
|
|
|
|
|
.catch((error) => Promise.reject(error)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private sendSecretToServiceWorker(secret: string): Promise<void> { |
|
|
const message: ServiceWorkerMessage = { |
|
|
const message: ServiceWorkerMessage = { |
|
|
type: "SEND_LOCAL_DATA", |
|
|
type: "SEND_LOCAL_DATA", |
|
|
data: secret, |
|
|
data: secret, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
this.sendMessageToServiceWorker(message) |
|
|
return this.sendMessageToServiceWorker(message).then((response) => { |
|
|
.then((response) => { |
|
|
|
|
|
// The service worker has received the message and responded |
|
|
|
|
|
console.log("Response from service worker:", response); |
|
|
console.log("Response from service worker:", response); |
|
|
// Perform the next action here |
|
|
}); |
|
|
// Check if Notifications are supported |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private checkNotificationSupport(): Promise<void> { |
|
|
if (!("Notification" in window)) { |
|
|
if (!("Notification" in window)) { |
|
|
alert("This browser does not support notifications."); |
|
|
alert("This browser does not support notifications."); |
|
|
return Promise.reject( |
|
|
return Promise.reject("This browser does not support notifications."); |
|
|
"This browser does not support notifications.", |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Check existing permissions |
|
|
|
|
|
if (Notification.permission === "granted") { |
|
|
if (Notification.permission === "granted") { |
|
|
return Promise.resolve("granted"); |
|
|
return Promise.resolve(); |
|
|
} |
|
|
} |
|
|
|
|
|
return Promise.resolve(); |
|
|
// Request permission |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
const permissionResult = Notification.requestPermission( |
|
|
|
|
|
(result) => { |
|
|
|
|
|
resolve(result); |
|
|
|
|
|
}, |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (permissionResult) { |
|
|
|
|
|
permissionResult.then(resolve, reject); |
|
|
|
|
|
} |
|
|
} |
|
|
}).then((permissionResult) => { |
|
|
|
|
|
console.log("Permission result:", permissionResult); |
|
|
|
|
|
|
|
|
|
|
|
if (permissionResult !== "granted") { |
|
|
private requestNotificationPermission(): Promise<NotificationPermission> { |
|
|
alert( |
|
|
return Notification.requestPermission().then((permission) => { |
|
|
"We need notification permission to provide certain features.", |
|
|
if (permission !== "granted") { |
|
|
); |
|
|
alert("We need notification permission to provide certain features."); |
|
|
return Promise.reject("We weren't granted permission."); |
|
|
throw new Error("We weren't granted permission."); |
|
|
} |
|
|
} |
|
|
|
|
|
return permission; |
|
|
return permissionResult; |
|
|
|
|
|
}); |
|
|
}); |
|
|
}) |
|
|
|
|
|
.catch((error) => { |
|
|
|
|
|
console.error("Error:", error); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async turnOnNotifications() { |
|
|
async turnOnNotifications() { |
|
|