Add: SignalR real-time notifications for admin panel

- Created NotificationHub for instant browser notifications
- Updated CryptoPaymentService to broadcast via SignalR
- Added JavaScript client with toast notifications
- Works with custom SSL certificates (no FCM dependency)
- Automatic reconnection with exponential backoff
- Notification sound and visual indicators
- Bypasses all Web Push SSL certificate issues
This commit is contained in:
2025-10-06 17:57:10 +01:00
parent b8390162d9
commit be91b3efd7
5 changed files with 272 additions and 5 deletions

View File

@@ -1,8 +1,10 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.SignalR;
using LittleShop.Data;
using LittleShop.Models;
using LittleShop.DTOs;
using LittleShop.Enums;
using LittleShop.Hubs;
namespace LittleShop.Services;
@@ -14,6 +16,7 @@ public class CryptoPaymentService : ICryptoPaymentService
private readonly IConfiguration _configuration;
private readonly IPushNotificationService _pushNotificationService;
private readonly ITeleBotMessagingService _teleBotMessagingService;
private readonly IHubContext<NotificationHub> _notificationHub;
public CryptoPaymentService(
LittleShopContext context,
@@ -21,7 +24,8 @@ public class CryptoPaymentService : ICryptoPaymentService
ILogger<CryptoPaymentService> logger,
IConfiguration configuration,
IPushNotificationService pushNotificationService,
ITeleBotMessagingService teleBotMessagingService)
ITeleBotMessagingService teleBotMessagingService,
IHubContext<NotificationHub> notificationHub)
{
_context = context;
_silverPayService = silverPayService;
@@ -29,8 +33,9 @@ public class CryptoPaymentService : ICryptoPaymentService
_configuration = configuration;
_pushNotificationService = pushNotificationService;
_teleBotMessagingService = teleBotMessagingService;
_notificationHub = notificationHub;
_logger.LogInformation("CryptoPaymentService initialized with SilverPAY");
_logger.LogInformation("CryptoPaymentService initialized with SilverPAY and SignalR notifications");
}
public async Task<CryptoPaymentDto> CreatePaymentAsync(Guid orderId, CryptoCurrency currency)
@@ -215,13 +220,26 @@ public class CryptoPaymentService : ICryptoPaymentService
var title = "💰 Payment Confirmed";
var body = $"Order #{orderId.ToString()[..8]} payment of £{amount:F2} confirmed. Ready for acceptance.";
// Send push notification to admin users
// Send SignalR real-time notification to connected admin users
await _notificationHub.Clients.All.SendAsync("ReceiveNotification", new
{
title = title,
message = body,
type = "payment",
orderId = orderId,
amount = amount,
timestamp = DateTime.UtcNow,
icon = "💰",
url = $"/Admin/Orders/Details/{orderId}"
});
// Send push notification to admin users (may not work with custom CA)
await _pushNotificationService.SendOrderNotificationAsync(orderId, title, body);
// Send TeleBot message to customer
await _teleBotMessagingService.SendPaymentConfirmedAsync(orderId);
_logger.LogInformation("Sent payment confirmation notifications for order {OrderId} (Admin + Customer)", orderId);
_logger.LogInformation("Sent payment confirmation notifications for order {OrderId} (SignalR + Push + Telegram)", orderId);
}
catch (Exception ex)
{