55 lines
1.3 KiB
JavaScript
55 lines
1.3 KiB
JavaScript
import webpush from "web-push";
|
|
import PouchDB from "./PouchDB.js";
|
|
|
|
if (!process.env.API_URL) {
|
|
throw new Error(`env API_URL is not set`);
|
|
}
|
|
|
|
const settings = await new PouchDB(`${process.env.API_URL}/server_settings`, {
|
|
skip_setup: true,
|
|
}).get("server_settings:message_delivery_method_web_push:vapid");
|
|
|
|
webpush.setVapidDetails(
|
|
settings.vapid.subject,
|
|
settings.vapid.keys.publicKey,
|
|
settings.vapid.keys.privateKey,
|
|
);
|
|
|
|
const notification = {
|
|
type: "notification",
|
|
notification: {
|
|
title: "test",
|
|
options: {
|
|
icon: "/images/qr-code-logo-200x200.jpg",
|
|
body: "Test push message from DevTools.",
|
|
tag: "2105ef6f-3ca6-4f99-b258-0ca23f014309",
|
|
// requireInteraction: true,
|
|
// silent: false,
|
|
// vibrate: [200, 100, 200, 100, 200, 100, 200],
|
|
data: {
|
|
url: "/chat/6ilhxu/13trvu",
|
|
payload: { tag: "2105ef6f-3ca6-4f99-b258-0ca23f014309" },
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const { rows } = await new PouchDB(
|
|
`${process.env.API_URL}/web_push_subscriptions`,
|
|
{ skip_setup: true },
|
|
).allDocs({
|
|
startkey: "web_push_subscription:",
|
|
endkey: "web_push_subscription:\uFFFF",
|
|
include_docs: true,
|
|
});
|
|
|
|
await Promise.allSettled(
|
|
rows.map(({ doc }) =>
|
|
webpush.sendNotification(doc.subscription, JSON.stringify(notification)),
|
|
),
|
|
);
|
|
|
|
// issues:
|
|
// 1. NO SOUND
|
|
// 2. SAFARI NO OPEN PAGE
|