From a8a50c76a8d4803179fd66f189a10a54b438f410 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 7 May 2024 11:31:59 +0300 Subject: [PATCH] basic support for push messages with actions --- components/PwaManager.tsx | 74 ++++++++++++++++++++++++++++++--------- pages/api/notify.ts | 4 +-- worker/index.js | 25 +++++++++++-- 3 files changed, 82 insertions(+), 21 deletions(-) diff --git a/components/PwaManager.tsx b/components/PwaManager.tsx index 623fd17..a7e3f5d 100644 --- a/components/PwaManager.tsx +++ b/components/PwaManager.tsx @@ -201,6 +201,42 @@ function PwaManager() { }); }; + async function sendTestReminder(event: MouseEvent): Promise { + event.preventDefault(); + if (!subscription) { + console.error('Web push not subscribed'); + return; + } + + await fetch('/api/notify', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ broadcast: true, message: 'Моля, въведете вашите предпочитания за юни до 25-ти май.' }) + }); + } + + async function sendTestCoverMe(event: MouseEvent): Promise { + event.preventDefault(); + if (!subscription) { + console.error('Web push not subscribed'); + return; + } + + await fetch('/api/notify', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + broadcast: true, message: "Брат ТЕСТ търси заместник за 24-ти май от 10:00 ч. Можеш ли да го покриеш?", + //use fontawesome icons for actions + actions: [{ action: 'covermeaccepted', title: 'Да ', icon: '✅' }] + }) + }); + } + return ( <>
@@ -216,24 +252,12 @@ function PwaManager() { {isPWAInstalled &&

App is installed!

} {isStandAlone &&

PWA App

} - - - -
+
+ + {notificationPermission !== "granted" && (