@@ -79,6 +87,29 @@ export default function NotificationPage() {
return (
Latest Updates
+
+ {status ? (
+
+ ) : (
+
+ )}
+
{notifications.map((notif) => {
return
;
})}
diff --git a/src/pages/Notification/notification-engine.ts b/src/pages/Notification/notification-engine.ts
new file mode 100644
index 0000000..3b51da7
--- /dev/null
+++ b/src/pages/Notification/notification-engine.ts
@@ -0,0 +1,61 @@
+class Notification {
+ static notify = window.Notification;
+
+ public static async requestPermission() {
+ if (!Notification.isSupported()) {
+ throw new Error("Notification API is not supported");
+ }
+ const permission = await Notification.notify.requestPermission();
+ console.log("Notification API permission", permission);
+ return permission;
+ }
+
+ public static isSupported() {
+ return Notification.notify !== undefined;
+ }
+}
+
+class BgService {
+ static bg = window.navigator;
+
+ public static isSupported() {
+ return BgService.bg !== undefined;
+ }
+
+ public static async registerServiceWorker(swPath: string) {
+ const registration = await BgService.bg.serviceWorker.register(swPath);
+ console.log("Service worker registered", registration);
+ return registration;
+ }
+
+ public static async unregisterServiceWorker() {
+ const registrations = await BgService.getRegistrations();
+ registrations.forEach((registration) => registration.unregister());
+ return registrations;
+ }
+
+ public static async getRegistrations() {
+ const registration = await BgService.bg.serviceWorker.getRegistrations();
+ console.log("Service worker registered", registration);
+ return registration;
+ }
+}
+
+export async function setup_notification() {
+ Notification.requestPermission();
+
+ const worker = await BgService.registerServiceWorker(
+ "https://team.dtutimes.com/notification-service.js",
+ );
+
+ console.log("Service worker registered", worker);
+}
+
+export async function disable_notification() {
+ await BgService.unregisterServiceWorker();
+}
+
+export async function setup_present() {
+ const services = await BgService.getRegistrations();
+ return services.length > 0;
+}
diff --git a/tsconfig.json b/tsconfig.json
index f8e9eef..e03b018 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -31,7 +31,8 @@
}
},
"include": [
- "src"
+ "src",
+ "public/notification-service.js"
],
"references": [
{