From 437e4e14886c1990d0362fcc016b1b6063bf88ac Mon Sep 17 00:00:00 2001 From: Jordan Millett Date: Fri, 8 Nov 2024 15:24:11 -0800 Subject: [PATCH] Update --- Pages/AdminPage.razor | 23 +++++++++++++++++++---- wwwroot/js/notifications.js | 32 ++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Pages/AdminPage.razor b/Pages/AdminPage.razor index 57bf0f3..ed095c6 100644 --- a/Pages/AdminPage.razor +++ b/Pages/AdminPage.razor @@ -2,6 +2,7 @@ @inject HttpClient Http @inject HelperService Helper @inject NotificationService Notifications +@inject IJSRuntime Runtime @using System.Text @using System.Text.Json; @@ -12,6 +13,11 @@

Admin Page



+@if(DeviceEndpoint != "") +{ +

@DeviceEndpoint

+} +
@@ -104,6 +110,15 @@ string SubLimit = ""; + string DeviceEndpoint = ""; + + protected override async Task OnInitializedAsync() + { + DeviceEndpoint = await Runtime.InvokeAsync("getEndpoint"); + if(DeviceEndpoint != "") + DeviceEndpoint = DeviceEndpoint[^10..]; + } + async Task GetLogs() { if (!await Notifications.ServerAlive()) @@ -115,10 +130,10 @@ try { WebLogRequest Data = new WebLogRequest - { - Count = int.TryParse(LogCount, out int count) ? count : 1, - Password = await Helper.Hash(Password) - }; + { + Count = int.TryParse(LogCount, out int count) ? count : 1, + Password = await Helper.Hash(Password) + }; HttpResponseMessage HttpResponse = await Http.PostAsJsonAsync("https://vetra.jordanmillett.net/api/logs", Data); if (HttpResponse.IsSuccessStatusCode) diff --git a/wwwroot/js/notifications.js b/wwwroot/js/notifications.js index 7a6936c..0fed5ab 100644 --- a/wwwroot/js/notifications.js +++ b/wwwroot/js/notifications.js @@ -60,10 +60,15 @@ async function updateNotificationStatus(staySubscribed) { const registration = await navigator.serviceWorker.register('service-worker.js'); - const subscription = await registration.pushManager.subscribe({ - userVisibleOnly: true, - applicationServerKey: 'BBIS4Xi3mCa3vxm7ymp5TWVgYhlTEMgf7vr1cn1eMsz3akiJ1WLIvA538FyJN4tTtBqKrTmu0t6McyZ6ISnY_qQ' // replace with your actual VAPID public key - }); + let subscription = await registration.pushManager.getSubscription(); + + if (!subscription) + { + const subscription = await registration.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: 'BBIS4Xi3mCa3vxm7ymp5TWVgYhlTEMgf7vr1cn1eMsz3akiJ1WLIvA538FyJN4tTtBqKrTmu0t6McyZ6ISnY_qQ' + }); + } const p256dh = subscription.getKey('p256dh'); const auth = subscription.getKey('auth'); @@ -107,3 +112,22 @@ async function updateNotificationStatus(staySubscribed) } } } + +async function getEndpoint() +{ + if ('serviceWorker' in navigator && 'PushManager' in window) + { + const registration = await navigator.serviceWorker.register('service-worker.js'); + + let subscription = await registration.pushManager.getSubscription(); + + if (!subscription) + return ""; + + const endpoint = subscription.endpoint; + + return endpoint.toString(); + } + + return ""; +} \ No newline at end of file