53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
/**
|
|
* Dev script: send a test push notification to all subscriptions for the dev user.
|
|
* Usage: tsx --env-file=.env scripts/send-test-push.mts
|
|
*/
|
|
import webPush from "web-push";
|
|
import postgres from "postgres";
|
|
|
|
const subject = process.env["VAPID_SUBJECT"];
|
|
const publicKey = process.env["VAPID_PUBLIC_KEY"];
|
|
const privateKey = process.env["VAPID_PRIVATE_KEY"];
|
|
const databaseUrl = process.env["DATABASE_URL"];
|
|
|
|
if (!subject || !publicKey || !privateKey) {
|
|
console.error("VAPID env vars not set");
|
|
process.exit(1);
|
|
}
|
|
if (!databaseUrl) {
|
|
console.error("DATABASE_URL not set");
|
|
process.exit(1);
|
|
}
|
|
|
|
webPush.setVapidDetails(subject, publicKey, privateKey);
|
|
|
|
const sql = postgres(databaseUrl);
|
|
|
|
const subs = await sql`SELECT id, user_id, endpoint, p256dh, auth FROM push_subscriptions`;
|
|
console.log(`Found ${subs.length} push subscription(s)`);
|
|
|
|
if (subs.length === 0) {
|
|
console.log("No subscriptions — enable push notifications in Settings first.");
|
|
await sql.end();
|
|
process.exit(0);
|
|
}
|
|
|
|
for (const sub of subs) {
|
|
try {
|
|
await webPush.sendNotification(
|
|
{ endpoint: sub.endpoint, keys: { p256dh: sub.p256dh, auth: sub.auth } },
|
|
JSON.stringify({
|
|
title: "famapp test",
|
|
body: "Push notifications are working!",
|
|
url: "/settings",
|
|
}),
|
|
);
|
|
console.log(`Sent to ${sub.endpoint.slice(0, 60)}...`);
|
|
} catch (err: unknown) {
|
|
const e = err as { statusCode?: number; message?: string };
|
|
console.error(`Failed (${e.statusCode ?? "?"}): ${e.message}`);
|
|
}
|
|
}
|
|
|
|
await sql.end();
|