Firebase_Implementation #135
@ -4,7 +4,7 @@
|
||||
{
|
||||
public string? Username { get; set; }
|
||||
public string? Password { get; set; }
|
||||
public required string DeviceToken { get; set; }
|
||||
public string? DeviceToken { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -52,18 +52,36 @@ namespace MarcoBMS.Services.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
var deviceToken = "ewjsd9zGTh6aS6Vg3Z_uxP:APA91bFZi1KzZdxlHUfBa_dX3PEJnDhX4R2dvFjD9Zf3WPSm957Hb53JPim7jrpjhpeOY61I9rfc11c3wpqWfW_06aSx-Yb8UfWpygV2YgZ8gbHtSku_PSQ";
|
||||
var message = new Message()
|
||||
{
|
||||
Token = "ewjsd9zGTh6aS6Vg3Z_uxP:APA91bFZi1KzZdxlHUfBa_dX3PEJnDhX4R2dvFjD9Zf3WPSm957Hb53JPim7jrpjhpeOY61I9rfc11c3wpqWfW_06aSx-Yb8UfWpygV2YgZ8gbHtSku_PSQ",
|
||||
Token = deviceToken,
|
||||
Notification = new Notification
|
||||
{
|
||||
Title = "Hello from .NET",
|
||||
Body = "This is a test message"
|
||||
}
|
||||
};
|
||||
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
|
||||
_logger.LogInfo("Firebase push notification messageId: {MessageId}", response);
|
||||
// Find user by email or phone number
|
||||
try
|
||||
{
|
||||
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
|
||||
_logger.LogInfo("Successfully sent message: {MessageId}", response);
|
||||
}
|
||||
catch (FirebaseMessagingException ex)
|
||||
{
|
||||
_logger.LogError("Error sending push notification. : {Error}", ex.Message);
|
||||
|
||||
// Check for the specific error codes that indicate an invalid token
|
||||
if (ex.MessagingErrorCode == MessagingErrorCode.Unregistered ||
|
||||
ex.MessagingErrorCode == MessagingErrorCode.InvalidArgument)
|
||||
{
|
||||
_logger.LogWarning("FCM token is invalid and should be deleted: {Token}", deviceToken);
|
||||
|
||||
// Add your logic here to remove the invalid token from your database
|
||||
// await YourTokenService.DeleteTokenAsync(recordAttendanceDot.DeviceToken);
|
||||
}
|
||||
}
|
||||
|
||||
var user = await _context.ApplicationUsers
|
||||
.FirstOrDefaultAsync(u => u.Email == loginDto.Username || u.PhoneNumber == loginDto.Username);
|
||||
|
||||
@ -139,11 +157,29 @@ namespace MarcoBMS.Services.Controllers
|
||||
Body = "This is a test message"
|
||||
}
|
||||
};
|
||||
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
|
||||
_logger.LogInfo("Firebase push notification messageId: {MessageId}", response);
|
||||
try
|
||||
{
|
||||
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
|
||||
_logger.LogInfo("Successfully sent message: {MessageId}", response);
|
||||
}
|
||||
catch (FirebaseMessagingException ex)
|
||||
{
|
||||
_logger.LogError("Error sending push notification. : {Error}", ex.Message);
|
||||
|
||||
// Check for the specific error codes that indicate an invalid token
|
||||
if (ex.MessagingErrorCode == MessagingErrorCode.Unregistered ||
|
||||
ex.MessagingErrorCode == MessagingErrorCode.InvalidArgument)
|
||||
{
|
||||
_logger.LogWarning("FCM token is invalid and should be deleted: {Token}", loginDto.DeviceToken);
|
||||
|
||||
// Add your logic here to remove the invalid token from your database
|
||||
// await YourTokenService.DeleteTokenAsync(recordAttendanceDot.DeviceToken);
|
||||
}
|
||||
}
|
||||
// Validate input DTO
|
||||
if (loginDto == null || string.IsNullOrWhiteSpace(loginDto.Username) || string.IsNullOrWhiteSpace(loginDto.Password))
|
||||
{
|
||||
_logger.LogWarning("Login failed: User not found for input {Username}", loginDto.Username ?? string.Empty);
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("Username or password is missing.", "Invalid request", 400));
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user