2026-01-02 12:25:44 +08:00
|
|
|
|
namespace WebApi.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
[ApiController]
|
2026-01-02 18:51:28 +08:00
|
|
|
|
[Route("api/[controller]/[action]")]
|
2026-01-02 12:25:44 +08:00
|
|
|
|
public class NotificationController(INotificationService notificationService) : ControllerBase
|
|
|
|
|
|
{
|
2026-01-02 18:51:28 +08:00
|
|
|
|
[HttpGet()]
|
|
|
|
|
|
public async Task<BaseResponse<string>> GetVapidPublicKey()
|
2026-01-02 12:25:44 +08:00
|
|
|
|
{
|
2026-01-02 18:51:28 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var key = await notificationService.GetVapidPublicKeyAsync();
|
|
|
|
|
|
return BaseResponse<string>.Done(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BaseResponse<string>.Fail(ex.Message);
|
|
|
|
|
|
}
|
2026-01-02 12:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 18:51:28 +08:00
|
|
|
|
public async Task<BaseResponse> Subscribe([FromBody] PushSubscriptionEntity subscription)
|
2026-01-02 12:25:44 +08:00
|
|
|
|
{
|
2026-01-02 18:51:28 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await notificationService.SubscribeAsync(subscription);
|
|
|
|
|
|
return BaseResponse.Done();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BaseResponse.Fail(ex.Message);
|
|
|
|
|
|
}
|
2026-01-02 12:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 18:51:28 +08:00
|
|
|
|
public async Task<BaseResponse> TestNotification([FromQuery] string message)
|
2026-01-02 12:25:44 +08:00
|
|
|
|
{
|
2026-01-02 18:51:28 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await notificationService.SendNotificationAsync(message);
|
|
|
|
|
|
return BaseResponse.Done();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BaseResponse.Fail(ex.Message);
|
|
|
|
|
|
}
|
2026-01-02 12:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|