Sending the data when marking the Attendance

This commit is contained in:
ashutosh.nehete 2025-08-13 16:33:19 +05:30
parent ba5698f4b2
commit 884efdce61

View File

@ -137,7 +137,59 @@ namespace Marco.Pms.Services.Service
.Where(ft => employeeIds.Contains(ft.EmployeeId) && ft.TenantId == tenantId)
.Select(ft => ft.FcmToken).ToListAsync();
await SendMessageToMultipleDevicesAsync(registrationTokens, notificationFirebase);
var data = new Dictionary<string, string>()
{
{ "Keyword", "Attendance" },
{ "ProjectId", projectId.ToString() },
{ "Action", markType.ToString() }
};
await SendMessageToMultipleDevicesWithDataAsync(registrationTokens, notificationFirebase, data);
}
public async Task SendMessageToMultipleDevicesWithDataAsync(List<string> registrationTokens, Notification notificationFirebase, Dictionary<string, string> data)
{
try
{
var message = new MulticastMessage()
{
Tokens = registrationTokens,
Data = data,
Notification = notificationFirebase
};
// Send the multicast message
var response = await FirebaseMessaging.DefaultInstance.SendEachForMulticastAsync(message);
_logger.LogInfo("{SuccessCount} messages were sent successfully.", response.SuccessCount);
if (response.FailureCount > 0)
{
var failedTokens = new List<string>();
for (int i = 0; i < response.Responses.Count; i++)
{
if (!response.Responses[i].IsSuccess)
{
failedTokens.Add(registrationTokens[i]);
}
}
_logger.LogInfo("List of tokens that caused failures: " + string.Join(", ", failedTokens));
}
}
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");
// TODO: Implement the logic here to remove the invalid token from your database.
// Example: await YourTokenService.DeleteTokenAsync(loginDto.DeviceToken);
}
}
}
public async Task SendMessageToMultipleDevicesAsync(List<string> registrationTokens, Notification notificationFirebase)
{