Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMillett committed Nov 8, 2024
1 parent 3cb4e97 commit 437e4e1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
23 changes: 19 additions & 4 deletions Pages/AdminPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@inject HttpClient Http
@inject HelperService Helper
@inject NotificationService Notifications
@inject IJSRuntime Runtime
@using System.Text
@using System.Text.Json;

Expand All @@ -12,6 +13,11 @@
<h1 class="text-center"><strong>Admin Page</strong></h1>
<hr><br>

@if(DeviceEndpoint != "")
{
<h4 class="text-center">@DeviceEndpoint</h4>
}

<div class="row text-center">
<div class="col-4"></div>
<div class="col-4">
Expand Down Expand Up @@ -104,6 +110,15 @@

string SubLimit = "";

string DeviceEndpoint = "";

protected override async Task OnInitializedAsync()
{
DeviceEndpoint = await Runtime.InvokeAsync<string>("getEndpoint");
if(DeviceEndpoint != "")
DeviceEndpoint = DeviceEndpoint[^10..];
}

async Task GetLogs()
{
if (!await Notifications.ServerAlive())
Expand All @@ -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)
Expand Down
32 changes: 28 additions & 4 deletions wwwroot/js/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 "";
}

0 comments on commit 437e4e1

Please sign in to comment.