diff --git a/public/notification-service.js b/public/notification-service.js index b89261a..acda637 100644 --- a/public/notification-service.js +++ b/public/notification-service.js @@ -1,4 +1,38 @@ -self.addEventListener("activate", async () => { - // This will be called only once when the service worker is activated. +/// + +/** @type {ServiceWorkerGlobalScope} */ +const service_worker = self; + +function urlB64ToUint8Array(base64String) { + const padding = "=".repeat((4 - (base64String.length % 4)) % 4); + const base64 = (base64String + padding).replace(/-/g, "+").replace(/_/g, "/"); + + const rawData = window.atob(base64); + const outputArray = new Uint8Array(rawData.length); + const outputData = outputArray.map((output, index) => + rawData.charCodeAt(index), + ); + + return outputData; +} + +service_worker.addEventListener("activate", async () => { console.log("Hello from service worker"); + try { + /** @type {PushSubscriptionOptionsInit} */ + const options = { + applicationServerKey: + "BCOsRaxpJeR0KyIPIg1rHx3pUtWVsGDGOxH65dDkqyU5ycF-CjPJxuqiXF4M0LpUMG_rk_YxSZX34uHbrV5umJQ", + userVisibleOnly: true, + }; + console.log("Hello from service worker second time"); + + const subscription = + await service_worker.registration.pushManager.subscribe(options); + + console.log("Subscription", subscription); + console.log(JSON.stringify(subscription)); + } catch (err) { + console.log("Error", err); + } }); diff --git a/src/pages/Notification/notification-engine.ts b/src/pages/Notification/notification-engine.ts index ed164cc..8f53a32 100644 --- a/src/pages/Notification/notification-engine.ts +++ b/src/pages/Notification/notification-engine.ts @@ -57,6 +57,7 @@ export class BgService { public static async unregisterServiceWorker() { const registrations = await BgService.bg.serviceWorker.getRegistrations(); registrations.forEach((registration) => registration.unregister()); + return registrations; } public static async getRegistration() {