91 lines
3.6 KiB
C#
91 lines
3.6 KiB
C#
using FirebaseAdmin.Messaging;
|
|
using Marco.Pms.DataAccess.Data;
|
|
using Marco.Pms.Services.Service.ServiceInterfaces;
|
|
using MarcoBMS.Services.Service;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Marco.Pms.Services.Service
|
|
{
|
|
public class FirebaseService : IFirebaseService
|
|
{
|
|
private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
|
|
private readonly ILoggingService _logger;
|
|
public FirebaseService(IDbContextFactory<ApplicationDbContext> dbContextFactory,
|
|
ILoggingService logger)
|
|
{
|
|
_dbContextFactory = dbContextFactory ?? throw new ArgumentNullException(nameof(dbContextFactory));
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
public async Task SendDemoMessages(Notification notificationFirebase)
|
|
{
|
|
string deviceToken = "";
|
|
var message = new Message()
|
|
{
|
|
Token = deviceToken,
|
|
Notification = notificationFirebase
|
|
};
|
|
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
|
|
}
|
|
|
|
public async Task SendMessageToMultipleDevicesAsync()
|
|
{
|
|
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
|
|
|
// List of device registration tokens to send the message to
|
|
var registrationTokens = await _context.FCMTokenMappings.Select(ft => ft.FcmToken).ToListAsync();
|
|
//var registrationTokens = new List<string>
|
|
//{
|
|
// "YOUR_REGISTRATION_TOKEN_1",
|
|
// "YOUR_REGISTRATION_TOKEN_2",
|
|
// // add up to 500 tokens
|
|
//};
|
|
|
|
var message = new MulticastMessage()
|
|
{
|
|
Tokens = registrationTokens,
|
|
Notification = new Notification
|
|
{
|
|
Title = "Testing from API",
|
|
Body = "This messages comes from FireBase Services"
|
|
}
|
|
};
|
|
try
|
|
{
|
|
// 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);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|