-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vishal <[email protected]>
- Loading branch information
1 parent
bbab1b4
commit 3f46d15
Showing
2 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters