66 lines
1.6 KiB
JavaScript
66 lines
1.6 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: "cancelNotification",
|
||
|
|
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(
|
||
|
|
process.env.WEB_PUSH_SUBSCRIPTION_ID
|
||
|
|
? {
|
||
|
|
key: process.env.WEB_PUSH_SUBSCRIPTION_ID,
|
||
|
|
include_docs: true,
|
||
|
|
}
|
||
|
|
: {
|
||
|
|
startkey: "web_push_subscription:",
|
||
|
|
endkey: "web_push_subscription:\uFFFF",
|
||
|
|
include_docs: true,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
await Promise.allSettled(
|
||
|
|
rows.map(async ({ doc }) => {
|
||
|
|
await webpush.sendNotification(
|
||
|
|
doc.subscription,
|
||
|
|
JSON.stringify(notification),
|
||
|
|
);
|
||
|
|
console.log("sent ", doc._id);
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
|
||
|
|
// issues:
|
||
|
|
// 1. NO SOUND
|
||
|
|
// 2. SAFARI NO OPEN PAGE
|