Skip to content

Commit

Permalink
feat: register custom service worker with firebase push notification …
Browse files Browse the repository at this point in the history
…initialization

- use injectManifest to customize service worker

- use injectRegister to control when it gets registered (after fetching firebase config)
  • Loading branch information
ruchamahabal committed Feb 26, 2024
1 parent a4d7c77 commit df1214e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
9 changes: 4 additions & 5 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@
window.frappePushNotification = new FrappePushNotification("hrms")
document.addEventListener("DOMContentLoaded", () => {
window.frappePushNotification
.appendConfigToServiceWorkerURL(
"/assets/hrms/frontend/frappe-push-notification-sw.js"
)
.appendConfigToServiceWorkerURL("/assets/hrms/frontend/sw.js")
.then((url) => {
navigator.serviceWorker
.register(url, {
Expand All @@ -196,7 +194,7 @@
window.frappePushNotification
.initialize(registration)
.then(() => {
console.log("Frappe Notification Initialized")
console.log("Frappe Push Notification initialized")
})
})
})
Expand All @@ -205,7 +203,7 @@
})
})
</script>
<!-- END ! Import Frappe Notification JS -->
<!-- END Import Frappe Notification JS -->
</head>
<body class="antialiased">
<div id="app"></div>
Expand All @@ -215,6 +213,7 @@
<script>
window.csrf_token = "{{ csrf_token }}"
</script>

<script type="module" src="/src/main.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { cleanupOutdatedCaches, precacheAndRoute } from "workbox-precaching"
import { clientsClaim } from "workbox-core"

import { initializeApp } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-app.js"
import {
getMessaging,
onBackgroundMessage,
} from "https://www.gstatic.com/firebasejs/10.7.1/firebase-messaging-sw.js"

// Use the precache manifest generated by Vite
precacheAndRoute(self.__WB_MANIFEST)

// Clean up old caches
cleanupOutdatedCaches()

const jsonConfig = new URL(location).searchParams.get("config")
const firebaseApp = initializeApp(JSON.parse(jsonConfig))
const messaging = getMessaging(firebaseApp)

function isChrome() {
return navigator.userAgent.toLowerCase().includes("chrome")
}

onBackgroundMessage(messaging, (payload) => {
const notificationTitle = payload.data.title
let notificationOptions = {
Expand All @@ -24,7 +35,7 @@ onBackgroundMessage(messaging, (payload) => {
notificationOptions["actions"] = [
{
action: payload.data.click_action,
title: "View details",
title: "View Details",
},
]
}
Expand All @@ -41,3 +52,6 @@ if (isChrome()) {
}
})
}

self.skipWaiting()
clientsClaim()
5 changes: 5 additions & 0 deletions frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ export default defineConfig({
vue(),
VitePWA({
registerType: "autoUpdate",
strategies: "injectManifest",
injectRegister: null,
devOptions: {
enabled: true,
type: "module",
navigateFallback: "index.html",
},
manifest: {
display: "standalone",
name: "Frappe HR",
short_name: "Frappe HR",
start_url: "/hrms",
description: "Everyday HR & Payroll operations at your fingertips",
theme_color: "#ffffff",
icons: [
{
src: "/assets/hrms/manifest/manifest-icon-192.maskable.png",
Expand Down

0 comments on commit df1214e

Please sign in to comment.