.NET Firebase-Fehler beim Senden der Push-BenachrichtigungC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 .NET Firebase-Fehler beim Senden der Push-Benachrichtigung

Post by Anonymous »

In der Anfrage fehlen die erforderlichen Authentifizierungsdaten. Erwartetes OAuth 2-Zugriffstoken, Anmeldecookie oder andere gültige Authentifizierungsdaten. Siehe
https://developers.google.com/identity/ ... le-project.
Problem
Als ich es gestern gesendet habe, hat es funktioniert, aber heute erhalte ich diese Fehlermeldung. Als ersten Schritt habe ich eine neue JSON-Datei in Firebase erstellt, erhalte jedoch immer noch die gleiche Fehlermeldung. Wie kann ich das beheben?

Code: Select all

public async Task SendPushNotificationAsync(string fcmToken, string title, string message)
{
if (string.IsNullOrWhiteSpace(fcmToken))
{
_logger.LogWarning("FCM token is null or empty");
throw new ArgumentException("FCM token cannot be null or empty", nameof(fcmToken));
}

if (FirebaseApp.DefaultInstance == null)
{
_logger.LogError("Firebase App is not initialized. DefaultInstance is null");
throw new ApplicationException("Firebase App is not initialized");
}

var notification = new Message
{
Token = fcmToken,
Notification = new FirebaseAdmin.Messaging.Notification
{
Title = title,
Body = message
},
Apns = new ApnsConfig()
{
Aps = new Aps()
{
Alert = new ApsAlert()
{
Title = title,
Body = message
},
Sound = "default",
Badge = 1,
},
}
};

try
{
_logger.LogInformation("Sending push notification to token: {Token}", fcmToken);
var messageId = await FirebaseMessaging.DefaultInstance.SendAsync(notification); // Error Line
_logger.LogInformation("Push notification sent successfully. Message ID: {MessageId}", messageId);
}
catch (Exception e)
{
_logger.LogError(e, "Error sending push notification to token {Token}", fcmToken);
throw new ApplicationException($"Error sending push notification: {e.Message}", e);
}
}
Dienst-CTOR:

Code: Select all

public FirebasePushService(IConfiguration configuration, ILogger logger, IWebHostEnvironment environment)
{
_logger = logger;

// Initialize Firebase Admin SDK if not already initialized
if (FirebaseApp.DefaultInstance == null)
{
var firebaseConfigPath = configuration["Firebase:ConfigPath"];

if (string.IsNullOrEmpty(firebaseConfigPath))
{
throw new ApplicationException("Firebase:ConfigPath configuration is missing");
}

// Try to find the file in multiple locations, using ContentRootPath as primary source
var possiblePaths = new[]
{
firebaseConfigPath,
Path.Combine(environment.ContentRootPath, firebaseConfigPath),
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, firebaseConfigPath),
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", firebaseConfigPath)
};

var configPath = possiblePaths.FirstOrDefault(File.Exists);

if (configPath == null)
{
_logger.LogError("Firebase configuration file not found in any of these locations: {Paths}",
string.Join(", ", possiblePaths));
throw new ApplicationException(
$"Firebase configuration file not found in any of these locations: {string.Join(", ", possiblePaths)}");
}

try
{
_logger.LogInformation("Initializing Firebase with config file: {ConfigPath}", configPath);
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromFile(configPath)
});
_logger.LogInformation("Firebase initialized successfully");
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to initialize Firebase Admin SDK");
throw;
}
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post