-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add infrastructure for push notifications
- Loading branch information
Showing
14 changed files
with
202 additions
and
43 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
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
Empty file.
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// only partially defined, see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration | ||
type serviceWorkerRegistration = {active: bool} | ||
|
||
type registerSWOptions = { | ||
immediate?: bool, | ||
onNeedRefresh?: unit => unit, | ||
onOfflineReady?: unit => unit, | ||
/** | ||
* Called once the service worker is registered (requires version `0.12.8+`). | ||
* | ||
* @param swScriptUrl The service worker script url. | ||
* @param registration The service worker registration if available. | ||
*/ | ||
onRegisteredSW?: (string, option<serviceWorkerRegistration>) => unit, | ||
onRegisterError?: Exn.t => unit, | ||
} | ||
|
||
@module("virtual:pwa-register") | ||
external registerSW: registerSWOptions => unit = "registerSW" | ||
|
||
let serviceWorkerRegistration: promise< | ||
serviceWorkerRegistration, | ||
> = %raw(`navigator.serviceWorker.ready`) |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { getMessaging, onBackgroundMessage } from "firebase/messaging/sw"; | ||
import { initializeApp } from "firebase/app"; | ||
import { clientsClaim } from "workbox-core"; | ||
import { cleanupOutdatedCaches, precacheAndRoute } from "workbox-precaching"; | ||
|
||
import { firebaseConfig } from "../backend/firebaseConfig"; | ||
|
||
declare const self: ServiceWorkerGlobalScope; | ||
|
||
self.skipWaiting(); | ||
clientsClaim(); | ||
|
||
cleanupOutdatedCaches(); | ||
|
||
precacheAndRoute(self.__WB_MANIFEST); | ||
|
||
self.addEventListener("message", (event) => { | ||
if (event.data && event.data.type === "SKIP_WAITING") self.skipWaiting(); | ||
}); | ||
|
||
const firebaseApp = initializeApp(firebaseConfig); | ||
const messaging = getMessaging(firebaseApp); | ||
onBackgroundMessage(messaging, (payload) => { | ||
const notificationTitle = payload.notification?.title; | ||
if (!notificationTitle) return; | ||
self.registration.showNotification(notificationTitle, { | ||
body: payload.notification!.body, | ||
icon: "/pwa-192.png", | ||
}); | ||
}); |
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
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
Oops, something went wrong.