Firebase_Implementation #135
@ -4,6 +4,12 @@
|
|||||||
{
|
{
|
||||||
public required string Username { get; set; }
|
public required string Username { get; set; }
|
||||||
public required string Password { get; set; }
|
public required string Password { get; set; }
|
||||||
public string? FcmToken { get; set; }
|
}
|
||||||
|
|
||||||
|
public class MobileLoginDto
|
||||||
|
{
|
||||||
|
public required string Username { get; set; }
|
||||||
|
public required string Password { get; set; }
|
||||||
|
public required string FcmToken { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,7 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
/// <returns>An IActionResult containing the authentication tokens or an error response.</returns>
|
/// <returns>An IActionResult containing the authentication tokens or an error response.</returns>
|
||||||
|
|
||||||
[HttpPost("login-mobile")]
|
[HttpPost("login-mobile")]
|
||||||
public async Task<IActionResult> LoginMobile([FromBody] LoginDto loginDto)
|
public async Task<IActionResult> LoginMobile([FromBody] MobileLoginDto loginDto)
|
||||||
{
|
{
|
||||||
// Log the start of the login attempt for traceability.
|
// Log the start of the login attempt for traceability.
|
||||||
_logger.LogInfo("Login attempt initiated for user: {Username}", loginDto.Username ?? "N/A");
|
_logger.LogInfo("Login attempt initiated for user: {Username}", loginDto.Username ?? "N/A");
|
||||||
|
@ -11,12 +11,12 @@ namespace Marco.Pms.Services.Service
|
|||||||
public class FirebaseService : IFirebaseService
|
public class FirebaseService : IFirebaseService
|
||||||
{
|
{
|
||||||
private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
|
private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
|
||||||
private readonly ILoggingService _logger;
|
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||||
public FirebaseService(IDbContextFactory<ApplicationDbContext> dbContextFactory,
|
public FirebaseService(IDbContextFactory<ApplicationDbContext> dbContextFactory,
|
||||||
ILoggingService logger)
|
IServiceScopeFactory serviceScopeFactory)
|
||||||
{
|
{
|
||||||
_dbContextFactory = dbContextFactory ?? throw new ArgumentNullException(nameof(dbContextFactory));
|
_dbContextFactory = dbContextFactory ?? throw new ArgumentNullException(nameof(dbContextFactory));
|
||||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
_serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendLoginMessageAsync(string name)
|
public async Task SendLoginMessageAsync(string name)
|
||||||
@ -37,7 +37,6 @@ namespace Marco.Pms.Services.Service
|
|||||||
public async Task SendAttendanceMessageAsync(Guid projectId, string Name, ATTENDANCE_MARK_TYPE markType, Guid tenantId)
|
public async Task SendAttendanceMessageAsync(Guid projectId, string Name, ATTENDANCE_MARK_TYPE markType, Guid tenantId)
|
||||||
{
|
{
|
||||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
||||||
|
|
||||||
var projectTask = Task.Run(async () =>
|
var projectTask = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
||||||
@ -143,7 +142,8 @@ namespace Marco.Pms.Services.Service
|
|||||||
}
|
}
|
||||||
public async Task SendMessageToMultipleDevicesWithDataAsync(List<string> registrationTokens, Notification notificationFirebase, Dictionary<string, string> data)
|
public async Task SendMessageToMultipleDevicesWithDataAsync(List<string> registrationTokens, Notification notificationFirebase, Dictionary<string, string> data)
|
||||||
{
|
{
|
||||||
|
using var scope = _serviceScopeFactory.CreateScope();
|
||||||
|
var _logger = scope.ServiceProvider.GetRequiredService<ILoggingService>();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var message = new MulticastMessage()
|
var message = new MulticastMessage()
|
||||||
@ -154,6 +154,7 @@ namespace Marco.Pms.Services.Service
|
|||||||
};
|
};
|
||||||
// Send the multicast message
|
// Send the multicast message
|
||||||
var response = await FirebaseMessaging.DefaultInstance.SendEachForMulticastAsync(message);
|
var response = await FirebaseMessaging.DefaultInstance.SendEachForMulticastAsync(message);
|
||||||
|
|
||||||
_logger.LogInfo("{SuccessCount} messages were sent successfully.", response.SuccessCount);
|
_logger.LogInfo("{SuccessCount} messages were sent successfully.", response.SuccessCount);
|
||||||
|
|
||||||
if (response.FailureCount > 0)
|
if (response.FailureCount > 0)
|
||||||
@ -184,11 +185,16 @@ namespace Marco.Pms.Services.Service
|
|||||||
// Example: await YourTokenService.DeleteTokenAsync(loginDto.DeviceToken);
|
// Example: await YourTokenService.DeleteTokenAsync(loginDto.DeviceToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Exception Occured while sending notification to firebase");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public async Task SendMessageToMultipleDevicesAsync(List<string> registrationTokens, Notification notificationFirebase)
|
public async Task SendMessageToMultipleDevicesAsync(List<string> registrationTokens, Notification notificationFirebase)
|
||||||
{
|
{
|
||||||
|
using var scope = _serviceScopeFactory.CreateScope();
|
||||||
|
var _logger = scope.ServiceProvider.GetRequiredService<ILoggingService>();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var message = new MulticastMessage()
|
var message = new MulticastMessage()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user