You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I need to use firebase admin SDK in an backend api. My project is using DI and it's not clear to me how to initialize the SDK to use methods from controllers - in particular, how to declare lifecycle when registering with services (session, scoped, singleton).
Is there a project example for this use case?
Thanks!
The text was updated successfully, but these errors were encountered:
Hi, I asked myself the same question) Apparently it's static and they don't have anything for DI. That's why I use it like this
public class NotificationService
{
private readonly ILogger<NotificationService> _logger;
private readonly IConfiguration _configuration;
public NotificationService(ILogger<NotificationService> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromFile(Path.Combine(AppContext.BaseDirectory, "firebase-adminsdk-creds.json"))
});
}
public async Task SendNotificationAsync(string token, string title, string body)
{
try
{
var message = new Message()
{
Token = token,
Notification = new Notification()
{
Title = title,
Body = body
}
};
await FirebaseMessaging.DefaultInstance.SendAsync(message);
}
catch (Exception ex)
{
_logger.LogError("Exception while sending a notification: " + ex.Message);
}
}
}
and add it to DI services.AddSingleton<NotificationService>();
It worked in my ASP.NET Core 8.0 WebApi app. If you are a perfectionist you can also use the interface like this public class FirebaseNotificationService : INotificationService
and add it to DI services.AddSingleton<INotificationService, FirebaseNotificationService>();
Hi,
I need to use firebase admin SDK in an backend api. My project is using DI and it's not clear to me how to initialize the SDK to use methods from controllers - in particular, how to declare lifecycle when registering with services (session, scoped, singleton).
Is there a project example for this use case?
Thanks!
The text was updated successfully, but these errors were encountered: