-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
39 lines (30 loc) · 805 Bytes
/
worker.js
File metadata and controls
39 lines (30 loc) · 805 Bytes
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
import webpush from "web-push";
const subscriptions = [];
const VAPID_PUBLIC = "YOUR_PUBLIC_KEY";
const VAPID_PRIVATE = "YOUR_PRIVATE_KEY";
webpush.setVapidDetails(
"mailto:test@test.com",
VAPID_PUBLIC,
VAPID_PRIVATE
);
export default {
async fetch(req) {
const url = new URL(req.url);
if (url.pathname === "/subscribe") {
const sub = await req.json();
subscriptions.push(sub);
return new Response("saved");
}
if (url.pathname === "/send") {
const payload = JSON.stringify({
title: "🔥 Push Working",
body: "Hello from Cloudflare Worker"
});
for (const sub of subscriptions) {
await webpush.sendNotification(sub, payload);
}
return new Response("sent");
}
return new Response("ok");
}
};