Skip to content

Commit

Permalink
feat: use showNotification for in-app notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchamahabal committed Feb 27, 2024
1 parent 6fea7ce commit 339097b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
1 change: 0 additions & 1 deletion frontend/public/frappe-push-notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ class FrappePushNotification {
* )} callback - Callback function to handle message
*/
onMessage(callback) {
console.log("onMessage")
if (callback == null) return
this.onMessageHandler = callback
if (this.messaging == null) return
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
<Toasts />

<InstallPrompt />
<FrappeNotification />
</ion-app>
</template>

<script setup>
import { onMounted } from "vue"
import { IonApp, IonRouterOutlet } from "@ionic/vue"
import { Toasts } from "frappe-ui"
import InstallPrompt from "@/components/InstallPrompt.vue"
import FrappeNotification from "@/components/FrappeNotification.vue"
import { showNotification } from "@/utils/pushNotifications"
onMounted(() => {
window?.frappePushNotification?.onMessage((payload) => {
showNotification(payload)
})
})
</script>
29 changes: 0 additions & 29 deletions frontend/src/components/FrappeNotification.vue

This file was deleted.

28 changes: 28 additions & 0 deletions frontend/src/utils/pushNotifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const isChrome = () =>
navigator.userAgent.toLowerCase().includes("chrome")

export const showNotification = (payload) => {
const registration = window.frappePushNotification.serviceWorkerRegistration
if (!registration) return

const notificationTitle = payload?.data?.title
const notificationOptions = {
body: payload?.data?.body || "",
}
if (isChrome()) {
notificationOptions["data"] = {
url: payload?.data?.click_action,
}
} else {
if (payload?.data?.click_action) {
notificationOptions["actions"] = [
{
action: payload.data.click_action,
title: "View Details",
},
]
}
}

registration.showNotification(notificationTitle, notificationOptions)
}

0 comments on commit 339097b

Please sign in to comment.