-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.ts
71 lines (66 loc) · 1.8 KB
/
worker.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function isMobile() {
//@ts-ignore
const mobile = self.navigator.userAgentData.mobile;
return mobile;
}
self.addEventListener("notificationclick", function (e) {
sendToMain((e as any).notification);
(e as any).notification.close();
// 알림 클릭 시 필요한 동작을 추가할 수 있습니다.
});
self.addEventListener("message", function (event) {
if (event.data) {
const { type, data } = event.data;
const { title, body, icon, id } = data;
// console.log("서비스 워커가 메시지를 받았습니다: ", {
// ...data,
// });
(self as any).clients.matchAll().then((clients) => {
clients.forEach((client) => {
client.postMessage({
type: "worker",
action: "todo/rerender",
data: {
title,
body,
icon,
id,
},
});
});
});
(event as any).waitUntil(
(self as any).registration
.showNotification(title, {
body,
icon,
data: { id },
tag: String(Date.now()),
})
.then(() => {
// (self as any).registration.getNotifications().then((values) => {
// if (!isMobile()) {
// setTimeout(() => {
// const notification = values[values.length - 1];
// notification.close();
// }, 5000);
// }
// });
})
);
}
});
function sendToMain(notification: Notification) {
const { title, body, icon, data } = notification;
if (data.id) {
(self as any).clients.matchAll().then((clients) => {
clients.forEach((client) => {
client.postMessage({
type: "worker",
action: "todo/view",
data: { title, body, icon, id: data.id },
});
});
});
}
}