first commit
This commit is contained in:
39
Services/NtfyNotificationService.cs
Normal file
39
Services/NtfyNotificationService.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using HanaToolbox.Config;
|
||||
using HanaToolbox.Logging;
|
||||
using HanaToolbox.Services.Interfaces;
|
||||
|
||||
namespace HanaToolbox.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Sends notifications to a ntfy server.
|
||||
/// URL and token are read from <see cref="NtfyConfig"/> (set during onboarding).
|
||||
/// Failures are silently swallowed — notifications must never crash the main flow.
|
||||
/// </summary>
|
||||
public sealed class NtfyNotificationService(NtfyConfig config, AppLogger logger)
|
||||
: INotificationService
|
||||
{
|
||||
private static readonly HttpClient Http = new();
|
||||
|
||||
public async Task SendAsync(string title, string message, CancellationToken ct = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(config.Url) || string.IsNullOrWhiteSpace(config.Token))
|
||||
{
|
||||
logger.Info("Ntfy not configured — skipping notification.");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, config.Url);
|
||||
request.Headers.Add("Authorization", $"Bearer {config.Token}");
|
||||
request.Headers.Add("Title", title);
|
||||
request.Content = new StringContent(message);
|
||||
await Http.SendAsync(request, ct);
|
||||
logger.Info($"Notification sent: [{title}] {message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Warning($"Failed to send ntfy notification: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user