From cc5a8c8569686a75d48c0cf18f52ff154a3e4b85 Mon Sep 17 00:00:00 2001 From: Stephen Gordon Date: Wed, 21 Feb 2024 15:41:40 +0000 Subject: [PATCH] testing --- sendPushNotification.js | 4 +- sendPushNotificationFirefox.js | 51 +++++++++++++++++++ .../SendNotification/SendNotification.tsx | 24 +++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 sendPushNotificationFirefox.js diff --git a/sendPushNotification.js b/sendPushNotification.js index e606a62..1c38dc4 100644 --- a/sendPushNotification.js +++ b/sendPushNotification.js @@ -32,14 +32,14 @@ const sendPushNotification = async () => { ); */ console.log(pushSubscription); try { - await fetch('https://payments-lyart.vercel.app/notifications', { + await fetch('https://payments-lyart.vercel.app/notification', { method: 'POST', headers: { 'Content-type': 'application/json', }, body: JSON.stringify({ subscription: pushSubscription, - message: 'Hello from script', + message: 'Hello from ste', }), signal: AbortSignal.timeout(10000), }); diff --git a/sendPushNotificationFirefox.js b/sendPushNotificationFirefox.js new file mode 100644 index 0000000..0f41133 --- /dev/null +++ b/sendPushNotificationFirefox.js @@ -0,0 +1,51 @@ +//import { NextRequest, NextResponse } from 'next/server'; +const webPush = require('web-push'); + +const sendPushNotification = async () => { + /* const publicKey = + 'BK9FZUL3u5bgvs8NlurUeFesIq5dm3qEUwOlh3hL7wGPbNec2SELGLwjKU2jWv9P9GULDvlWlC04Lric-w8yEf8'; + const privateKey = 'EctJtRAxWnq18ayGbnjcHQ'; */ + const publicKey = + 'BDvbxVcRlXNePbMsC7qHV77hJSuOB5vlxEB9Q0RHAHVY1RX2aaKx6gTG7jn2CUcbp9y25XcRfQVgR0gUhLaPzYc'; + const privateKey = 'KPlsUDm53-TTmJsK7Z14xw'; + /* webPush.setVapidDetails( + 'mailto:example@yourdomain.org', + publicKey, + privateKey + ); */ + const pushSubscription = { + endpoint: + 'https://updates.push.services.mozilla.com/wpush/v2/gAAAAABl1KyryrbEsojoejhhY9xbKAKH4dax-lQ5i9rH65Q4eNAeQAfiDby2XvRwb2tQzcXIthNlpc29TYndS1QdoQuxRhrfdQimMA-t1_zK6f-VWVfLuQpxWWoev60jJsWDM2-MDOB9VbRhwokwdi6lwqqtATHVkFCapTJc3ZiLwz7dOFeC1KM', + expirationTime: Math.floor(Date.now() / 1000) + 10, + + keys: { + auth: privateKey, + p256dh: publicKey, + }, + }; + /* const response = await webPush.sendNotification( + pushSubscription, + JSON.stringify({ + title: 'Hello from script', + message: 'Your web push notification is here!', + }) + ); */ + console.log(pushSubscription); + try { + await fetch('https://payments-lyart.vercel.app/notification', { + method: 'POST', + headers: { + 'Content-type': 'application/json', + }, + body: JSON.stringify({ + subscription: pushSubscription, + message: 'Hello from ste', + }), + signal: AbortSignal.timeout(10000), + }); + } catch (error) { + console.log(error); + } +}; + +sendPushNotification(); diff --git a/src/app/components/SendNotification/SendNotification.tsx b/src/app/components/SendNotification/SendNotification.tsx index 4b968e4..03d537c 100644 --- a/src/app/components/SendNotification/SendNotification.tsx +++ b/src/app/components/SendNotification/SendNotification.tsx @@ -1,5 +1,6 @@ // app/SendNotification.tsx 'use client'; +import useGetAddress from '@/app/hooks/useGetAddress'; import type { MouseEventHandler } from 'react'; import { useEffect, useState } from 'react'; @@ -25,6 +26,26 @@ export default function SendNotification() { const [registration, setRegistration] = useState(null); + const address = useGetAddress(); + + const createUser = async (sub: any) => { + try { + console.log('going to create user'); + await fetch('http://localhost:3002/api/users/register', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + address: address, + subscription: sub, + }), + }); + } catch (error) { + console.error('Error:', error); + } + }; + useEffect(() => { if ( typeof window !== 'undefined' && @@ -44,6 +65,8 @@ export default function SendNotification() { setSubscription(sub); console.log('suuuuub', sub); setIsSubscribed(true); + + createUser(sub); } }); setRegistration(reg); @@ -71,6 +94,7 @@ export default function SendNotification() { // TODO: you should call your API to save subscription data on the server in order to send web push notification from the server setSubscription(sub); setIsSubscribed(true); + createUser(sub); setText('Web push subscribed!'); alert('Web push subscribed!'); console.log(sub);