-
Notifications
You must be signed in to change notification settings - Fork 3
/
WebPush.js
26 lines (22 loc) · 901 Bytes
/
WebPush.js
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
import webpush from "https://code4fukui.github.io/web-push/src/index.js";
//import webpush from "../../util/web-push/src/index.js";
const getVAPIDKeys = async () => {
const fn = "data/vapidKeys.json";
return JSON.parse(await Deno.readTextFile(fn));
};
const vapidKeys = await getVAPIDKeys();
webpush.setVapidDetails(vapidKeys.mailaddress, vapidKeys.publicKey, vapidKeys.privateKey);
export const push = async (subscription, data) => {
if (typeof subscription == "string") {
if (subscription.endsWith(".json")) {
subscription = subscription.substring(0, subscription.length - 5);
}
subscription = JSON.parse(await Deno.readTextFile("data/subscription/" + subscription + ".json"));
}
if (typeof data == "string") {
data = { title: "WebPush", body: data };
}
await webpush.sendNotification(subscription, data);
};
const WebPush = { push };
export default WebPush;