@@ -44,6 +44,7 @@ async function requestNotificationPermission() { } const registration = await navigator.serviceWorker.ready + const subscription = await registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: await applicationServerKeyPromise, diff --git a/services/message-delivery-method-web-push/generate-keys.js b/services/message-delivery-method-web-push/generate-keys.js index 6a37773..2d62381 100644 --- a/services/message-delivery-method-web-push/generate-keys.js +++ b/services/message-delivery-method-web-push/generate-keys.js @@ -8,10 +8,10 @@ if (!process.env.VAPID_SUBJECT) { const vapidKeys = webpush.generateVAPIDKeys(); await new PouchDB(`${process.env.API_URL}/server_settings`, { - skip_setup: true, -}).put({ - _id: "server_settings:message_delivery_method_web_push:vapid", - vapid: { + _id: "server_settings:message_delivery_method_web_push_vapid", + type: "server_settings", + name: "message_delivery_method_web_push_vapid", + value: { subject: process.env.VAPID_SUBJECT, keys: vapidKeys, }, @@ -25,3 +25,14 @@ await new PouchDB(`${process.env.API_URL}/public_settings`, { name: "message_delivery_method_web_push_vapid_public_key", value: vapidKeys.publicKey, }); + +if (process.env.GCM_API_KEY) { + await new PouchDB(`${process.env.API_URL}/public_settings`, { + skip_setup: true, + }).put({ + _id: "server_settings:message_delivery_method_web_push_gcm_api_key", + type: "server_settings", + name: "message_delivery_method_web_push_gcm_api_key", + value: process.env.GCM_API_KEY, + }); +} diff --git a/services/message-delivery-method-web-push/test-send-notification.js b/services/message-delivery-method-web-push/test-send-notification.js index d3c5a21..8e83c59 100644 --- a/services/message-delivery-method-web-push/test-send-notification.js +++ b/services/message-delivery-method-web-push/test-send-notification.js @@ -5,44 +5,54 @@ 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`, { +const { + rows: [ + { + doc: { value: vapidDetails }, + }, + { doc: gcmApiSettingsDoc }, + ], +} = await new PouchDB(`${process.env.API_URL}/server_settings`, { skip_setup: true, -}).get("server_settings:message_delivery_method_web_push:vapid"); +}).allDocs({ + keys: [ + "server_settings:message_delivery_method_web_push_vapid", + "public_settings:message_delivery_method_web_push_gcm_api_key", + ], + include_docs: true, +}); webpush.setVapidDetails( - settings.vapid.subject, - settings.vapid.keys.publicKey, - settings.vapid.keys.privateKey, + vapidDetails.subject, + vapidDetails.keys.publicKey, + vapidDetails.keys.privateKey, ); +if (gcmApiSettingsDoc) { + console.log(gcmApiSettingsDoc.value); + webpush.setGCMAPIKey(gcmApiSettingsDoc.value); +} + +// optionally provide an identifier that the push service uses to coalesce notifications +const topic = "chat-6ilhxu-13trvu"; + const notification = { type: "notification", notification: { title: "test", options: { icon: "/images/qr-code-logo-200x200.jpg", - body: "Test push message from DevTools.", + body: "Test push message from server.", tag: "01936062-d10f-7f2f-8ae3-dd7f9fbc5c7f", - vibrate: [200, 100, 200, 100, 200, 100, 200], - data: { - url: "http://localhost:5173/chat/6ilhxu/13trvu", - payload: { tag: "01936062-d10f-7f2f-8ae3-dd7f9fbc5c7f" }, - }, - }, - }, -}; - -const notification2 = { - type: "notification", - notification: { - title: "test", - options: { - icon: "/images/qr-code-logo-200x200.jpg", - body: "Test push message from DevTools.", - tag: "01936062-d10f-7f2f-8ae3-dd7f9fbc5c7f", - // requireInteraction: true, + requireInteraction: true, // silent: false, // vibrate: [200, 100, 200, 100, 200, 100, 200], + // actions: [ + // { + // action: "open_url", + // title: "Открыть", + // }, + // ], data: { url: "/chat/6ilhxu/13trvu", payload: { tag: "01936062-d10f-7f2f-8ae3-dd7f9fbc5c7f" }, @@ -72,6 +82,10 @@ const result = await Promise.allSettled( await webpush.sendNotification( doc.subscription, JSON.stringify(notification), + { + urgency: "high", + topic, + }, ); console.log("sent ", doc._id); }),