1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import webPush, { PushSubscription as WebPushSubscriptionJSON } from "npm:web-push";
export type PushParams = Parameters<ServiceWorkerRegistration["showNotification"]>;
export default async (
{ url, pubKey, privKey }: { url: string; pubKey: string; privKey: string },
subscription?: PushSubscriptionJSON,
...params: Parameters<ServiceWorkerRegistration["showNotification"]>
) => {
webPush.setVapidDetails(url, pubKey, privKey);
if (!subscription) throw new Error("subscription is falsy");
await webPush.sendNotification(
subscription as WebPushSubscriptionJSON,
JSON.stringify(params),
{ TTL: 3600 },
);
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 28, 2024