From 48fb5fd4498b552dd0077352d58a7b0f19642a40 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Wed, 13 Aug 2025 10:21:07 +0530 Subject: [PATCH] Sending the notification after sending the response --- .../{DeviceTokenDto.cs => FCMTokenDto.cs} | 2 +- .../Controllers/AuthController.cs | 94 +++++++++++-------- 2 files changed, 58 insertions(+), 38 deletions(-) rename Marco.Pms.Model/Utilities/{DeviceTokenDto.cs => FCMTokenDto.cs} (77%) diff --git a/Marco.Pms.Model/Utilities/DeviceTokenDto.cs b/Marco.Pms.Model/Utilities/FCMTokenDto.cs similarity index 77% rename from Marco.Pms.Model/Utilities/DeviceTokenDto.cs rename to Marco.Pms.Model/Utilities/FCMTokenDto.cs index f80b01a..074225d 100644 --- a/Marco.Pms.Model/Utilities/DeviceTokenDto.cs +++ b/Marco.Pms.Model/Utilities/FCMTokenDto.cs @@ -1,6 +1,6 @@ namespace Marco.Pms.Model.Utilities { - public class DeviceTokenDto + public class FCMTokenDto { public required string FcmToken { get; set; } } diff --git a/Marco.Pms.Services/Controllers/AuthController.cs b/Marco.Pms.Services/Controllers/AuthController.cs index 91cc82f..b817274 100644 --- a/Marco.Pms.Services/Controllers/AuthController.cs +++ b/Marco.Pms.Services/Controllers/AuthController.cs @@ -158,41 +158,6 @@ namespace MarcoBMS.Services.Controllers // Log the start of the login attempt for traceability. _logger.LogInfo("Login attempt initiated for user: {Username}", loginDto.Username ?? "N/A"); - // --- Push Notification Section --- - // This section attempts to send a test push notification to the user's device. - // It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens. - var message = new Message() - { - Token = loginDto.DeviceToken, - Notification = new Notification - { - Title = "Hello from .NET", - Body = "This is a test message" - } - }; - - try - { - // Attempt to send the message via Firebase. - string response = await FirebaseMessaging.DefaultInstance.SendAsync(message); - _logger.LogInfo("Successfully sent test push notification. MessageId: {MessageId}", response); - } - catch (FirebaseMessagingException ex) - { - // Log the specific Firebase error. - _logger.LogError(ex, "Error sending push notification"); - - // Check for specific error codes that indicate an invalid or unregistered token. - if (ex.MessagingErrorCode == MessagingErrorCode.Unregistered || - ex.MessagingErrorCode == MessagingErrorCode.InvalidArgument) - { - _logger.LogWarning("FCM token is invalid and should be deleted from the database: {Token}", loginDto.DeviceToken ?? string.Empty); - - // TODO: Implement the logic here to remove the invalid token from your database. - // Example: await YourTokenService.DeleteTokenAsync(loginDto.DeviceToken); - } - } - try { // --- Input Validation --- @@ -285,6 +250,44 @@ namespace MarcoBMS.Services.Controllers // Return a successful response with the generated tokens. _logger.LogInfo("User {Username} logged in successfully.", user.UserName); + + _ = Task.Run(async () => + { + // --- Push Notification Section --- + // This section attempts to send a test push notification to the user's device. + // It's designed to fail gracefully and handle invalid Firebase Cloud Messaging (FCM) tokens. + var message = new Message() + { + Token = loginDto.DeviceToken, + Notification = new Notification + { + Title = "Hello from .NET", + Body = "This is a test message" + } + }; + + try + { + // Attempt to send the message via Firebase. + string response = await FirebaseMessaging.DefaultInstance.SendAsync(message); + _logger.LogInfo("Successfully sent test push notification. MessageId: {MessageId}", response); + } + catch (FirebaseMessagingException ex) + { + // Log the specific Firebase error. + _logger.LogError(ex, "Error sending push notification"); + + // Check for specific error codes that indicate an invalid or unregistered token. + if (ex.MessagingErrorCode == MessagingErrorCode.Unregistered || + ex.MessagingErrorCode == MessagingErrorCode.InvalidArgument) + { + _logger.LogWarning("FCM token is invalid and should be deleted from the database: {Token}", loginDto.DeviceToken ?? string.Empty); + + // TODO: Implement the logic here to remove the invalid token from your database. + // Example: await YourTokenService.DeleteTokenAsync(loginDto.DeviceToken); + } + } + }); return Ok(ApiResponse.SuccessResponse(responseData, "User logged in successfully.", 200)); } catch (Exception ex) @@ -921,9 +924,26 @@ namespace MarcoBMS.Services.Controllers [Authorize] [HttpPost("set/device-token")] - public async Task StoreDeviceToken([FromBody] DeviceTokenDto model) + public async Task StoreDeviceToken([FromBody] FCMTokenDto model) { - return Ok(ApiResponse.SuccessResponse(model)); + var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); + var tenantId = _userHelper.GetTenantId(); + var fcmTokenMapping = new FCMTokenMapping + { + EmployeeId = loggedInEmployee.Id, + FcmToken = model.FcmToken, + TenantId = tenantId + }; + //_context.FCMTokenMappings.Add(fcmTokenMapping); + //try + //{ + // await _context.SaveChangesAsync(); + //} + //catch (Exception ex) + //{ + // _logger.LogError(ex, "Exception occured while saving FCM Token for employee {EmployeeId}", loggedInEmployee.Id); + //} + return Ok(ApiResponse.SuccessResponse(fcmTokenMapping)); } private static string ComputeSha256Hash(string rawData) {