changed var to explicit type

changed title on SendMessageForm
handling error codes
added credentials handling
added icon to context menu item
This commit is contained in:
seba
2024-12-18 20:39:19 +01:00
parent c7e02655b0
commit 2c18f91d68
6 changed files with 106 additions and 18 deletions

View File

@@ -352,10 +352,24 @@ namespace ntfysh_client.Notifications
SubscribedTopicsByUnique.Remove(topicUniqueString);
}
public async Task<HttpResponseMessage> SendNotification(string host, NtfyEvent message)
public async Task SendNotification(string key, string title, string message)
{
var httpClient = new HttpClient();
return await httpClient.PostAsJsonAsync<NtfyEvent>(host, message);
if (SubscribedTopicsByUnique.TryGetValue(key, out SubscribedTopic topic))
{
HttpClient httpClient = new HttpClient();
PublishEvent notification = new PublishEvent();
notification.Title = title;
notification.Message = message;
notification.Topic = topic.TopicId;
if (!string.IsNullOrEmpty(topic.Username) || !string.IsNullOrEmpty(topic.Password))
{
string value = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{topic.Username}:{topic.Password}"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", value);
}
HttpResponseMessage response = await httpClient.PostAsJsonAsync<PublishEvent>(topic.ServerUrl.Replace("wss://", "https://"), notification);
response.EnsureSuccessStatusCode();
}
}
}
}

View File

@@ -0,0 +1,50 @@
using Newtonsoft.Json;
using System.Text.Json.Nodes;
namespace ntfysh_client.Notifications
{
internal class PublishEvent
{
[JsonProperty("topic")]
public string Topic { get; set; } = null!;
[JsonProperty("message")]
public string? Message { get; set; } = null;
[JsonProperty("title")]
public string? Title { get; set; } = null;
[JsonProperty("tags")]
public string[] Tags { get; set; } = null!;
[JsonProperty("priority")]
public NotificationPriority? Priority { get; set; } = null;
[JsonProperty("actions")]
public JsonArray? Actions { get; set; } = null;
[JsonProperty("click")]
public string? Click { get; set; } = null;
[JsonProperty("attach")]
public string? Attach { get; set; } = null;
[JsonProperty("markdown")]
public bool? Markdown { get; set; } = null;
[JsonProperty("icon")]
public string? Icon { get; set; } = null;
[JsonProperty("filename")]
public string? Filename { get; set; } = null;
[JsonProperty("delay")]
public string? Delay { get; set; } = null;
[JsonProperty("email")]
public string? Email { get; set; } = null;
[JsonProperty("call")]
public string? Call { get; set; } = null;
}
}