From 0dc3c8f552095ee0fc54f279cc44af2a144ee530 Mon Sep 17 00:00:00 2001 From: Vishal Date: Tue, 27 Aug 2024 11:16:15 +0530 Subject: [PATCH] refine error handling Signed-off-by: Vishal --- src/pages/Notification/notification-engine.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/pages/Notification/notification-engine.ts b/src/pages/Notification/notification-engine.ts index ff52404..4097b63 100644 --- a/src/pages/Notification/notification-engine.ts +++ b/src/pages/Notification/notification-engine.ts @@ -5,7 +5,12 @@ class Notification { if (!Notification.isSupported()) { throw new Error("Notification API is not supported"); } + const permission = await Notification.notify.requestPermission(); + if (permission !== "granted") { + throw new Error("Notification permission denied"); + } + console.log("Notification API permission", permission); return permission; } @@ -13,6 +18,10 @@ class Notification { public static isSupported() { return Notification.notify !== undefined; } + + public static permissionGranted() { + return Notification.notify.permission === "granted"; + } } class BgService { @@ -67,5 +76,5 @@ export async function disable_notification() { export async function setup_present() { const services = await BgService.getRegistrations(); - return services.length > 0; + return Notification.permissionGranted() && services.length > 0; }