feat: 落地 V1 全部源码 — WebSocket 通知 + 应用内历史查看
17 个源文件 + .gitignore + 默认 config.json 模板,严格按方案文档第 6 节实现。
用户核心场景:
- Windows 接收 Gotify 通知(WS /stream → AppNotificationBuilder)
- 应用内查看历史消息(打开 MainWindow 触发 GetRecentMessagesAsync,实时通过 MessageReceived 事件追加)
- 列表条目双击打开 extras.client::notification.click.url
关键变更点:
- GotifyMessage.Extras 类型由 Dictionary<string,object> 改为 Dictionary<string,JsonElement>,避免 System.Text.Json 反序列化时丢失嵌套结构
- 新增 HistoryConfig.ViewerLimit(默认 50,UI 硬上限 200)
- GotifyApiService.GetRecentMessagesAsync:since=0 拉最近 N 条,OrderByDescending(Id) 保证顺序
- GotifyWebSocketService 新增 MessageReceived 事件,MainWindow 用 Dispatcher.Invoke 切回 UI 线程追加
- NotificationService.AddArgument('url=...') 透传 click URL 给 OnNotificationInvoked
- MainWindow.xaml 增加 ListView + 刷新按钮,使用 ObservableCollection<GotifyMessage>
- 默认 config.json 用 https://gotify.example.com 占位 URL,ClientToken 留空,首启动自动写入 %LOCALAPPDATA%
依赖:Microsoft.WindowsAppSDK 2.4.0(方案文档固定版本)。
构建:本机 .NET 8 SDK 未安装,build 待用户在装好 SDK 后执行: dotnet restore && dotnet build -c Release
This commit is contained in:
+147
@@ -0,0 +1,147 @@
|
||||
using System.Diagnostics;
|
||||
using System.Windows;
|
||||
using Microsoft.Windows.AppNotifications;
|
||||
|
||||
namespace WindowsGotify;
|
||||
|
||||
public partial class App : Application
|
||||
{
|
||||
private static Mutex? _mutex;
|
||||
private Services.ConfigService? _configService;
|
||||
private Services.Logger? _logger;
|
||||
private Services.StateService? _stateService;
|
||||
private Services.GotifyApiService? _apiService;
|
||||
private Services.GotifyWebSocketService? _webSocketService;
|
||||
private Services.NotificationService? _notificationService;
|
||||
private Tray.TrayIconService? _trayService;
|
||||
private MainWindow? _mainWindow;
|
||||
|
||||
protected override async void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
base.OnStartup(e);
|
||||
|
||||
_mutex = new Mutex(true, "WindowsGotifyAgent-SingleInstance", out bool createdNew);
|
||||
if (!createdNew)
|
||||
{
|
||||
Shutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
_logger = new Services.Logger();
|
||||
_configService = new Services.ConfigService(_logger);
|
||||
var config = await _configService.LoadAsync();
|
||||
|
||||
_stateService = new Services.StateService(_logger);
|
||||
_apiService = new Services.GotifyApiService(config, _logger);
|
||||
_notificationService = new Services.NotificationService(config, _logger);
|
||||
|
||||
try
|
||||
{
|
||||
// NotificationInvoked 必须在 Register 之前注册。
|
||||
AppNotificationManager.Default.NotificationInvoked += OnNotificationInvoked;
|
||||
AppNotificationManager.Default.Register();
|
||||
_logger.Info("Windows App SDK notification manager registered.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("Notification registration failed.", ex);
|
||||
}
|
||||
|
||||
_mainWindow = new MainWindow();
|
||||
_mainWindow.Hide();
|
||||
|
||||
_trayService = new Tray.TrayIconService(
|
||||
onShowSettings: () => _mainWindow.ShowFromTray(),
|
||||
onTest: async () => await SendTestNotificationAsync(),
|
||||
onReconnect: () => _webSocketService?.RequestReconnect(),
|
||||
onExit: () => Shutdown()
|
||||
);
|
||||
|
||||
_webSocketService = new Services.GotifyWebSocketService(
|
||||
config,
|
||||
_logger,
|
||||
_stateService,
|
||||
_apiService,
|
||||
_notificationService,
|
||||
status => _trayService.SetStatus(status));
|
||||
|
||||
_mainWindow.Initialize(
|
||||
config,
|
||||
_configService,
|
||||
_logger,
|
||||
_trayService,
|
||||
_webSocketService,
|
||||
_apiService);
|
||||
|
||||
if (config.Startup.Enabled)
|
||||
{
|
||||
Utils.StartupManager.EnableCurrentUserStartup(_logger);
|
||||
}
|
||||
|
||||
await _webSocketService.StartAsync();
|
||||
}
|
||||
|
||||
private async Task SendTestNotificationAsync()
|
||||
{
|
||||
if (_notificationService is null)
|
||||
return;
|
||||
|
||||
await _notificationService.ShowAsync(
|
||||
new Models.GotifyMessage
|
||||
{
|
||||
Id = 0,
|
||||
AppId = 0,
|
||||
Message = "Windows Gotify Agent 测试通知。",
|
||||
Title = "Gotify 测试",
|
||||
Priority = 5,
|
||||
Date = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
});
|
||||
}
|
||||
|
||||
private void OnNotificationInvoked(
|
||||
AppNotificationManager sender,
|
||||
AppNotificationActivatedEventArgs args)
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var arguments = args.Arguments;
|
||||
if (arguments.StartsWith("url=", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var url = Uri.UnescapeDataString(arguments[4..]);
|
||||
Process.Start(new ProcessStartInfo
|
||||
{
|
||||
FileName = url,
|
||||
UseShellExecute = true
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
_mainWindow?.ShowFromTray();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.Error("Notification invocation failed.", ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected override void OnExit(ExitEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
_webSocketService?.Stop();
|
||||
_trayService?.Dispose();
|
||||
AppNotificationManager.Default.Unregister();
|
||||
AppNotificationManager.Default.NotificationInvoked -= OnNotificationInvoked;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.Error("Application cleanup failed.", ex);
|
||||
}
|
||||
|
||||
base.OnExit(e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user