Skip to content

Commit

Permalink
Merge pull request #163 from dvishal485/main
Browse files Browse the repository at this point in the history
update notif code
  • Loading branch information
dvishal485 authored Aug 26, 2024
2 parents bc7b387 + 3f46d15 commit 968a546
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
38 changes: 36 additions & 2 deletions public/notification-service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
self.addEventListener("activate", async () => {
// This will be called only once when the service worker is activated.
/// <reference lib="webworker" />

/** @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);
}
});
1 change: 1 addition & 0 deletions src/pages/Notification/notification-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 968a546

Please sign in to comment.