-
-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathpushNotifications.ts
More file actions
50 lines (42 loc) · 1.27 KB
/
pushNotifications.ts
File metadata and controls
50 lines (42 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
export const isChrome = () =>
navigator.userAgent.toLowerCase().includes("chrome")
export const showNotification = (payload: any) => {
// @ts-ignore
const registration = window.frappePushNotification.serviceWorkerRegistration
if (!registration) return
const data = payload?.data ?? {}
const notification = payload?.notification ?? {}
const notificationTitle = data.title || notification.title
const notificationOptions = {
body: payload?.notification?.body || "",
}
if (data.image) {
// @ts-ignore
notificationOptions["icon"] = data.image
}
if (data.creation) {
// @ts-ignore
notificationOptions["timestamp"] = data.creation
}
let url = `${data.base_url}/raven/channel/${data.channel_id}`
if (data.message_url) {
url = data.message_url
}
if (isChrome()) {
// @ts-ignore
notificationOptions["data"] = {
url: url,
}
} else {
if (data.click_action) {
// @ts-ignore
notificationOptions["actions"] = [
{
action: url,
title: "View",
},
]
}
}
registration.showNotification(notificationTitle, notificationOptions)
}