Hi, Firebase dashboard is not registering any logs for my MAUI app. I am using Xamarin.firebase.Analytics for Android and AdamE.Firebase.iOS.Analytics for iOS.
.csproj
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-ios'">
<BundleResource Include="GoogleService-Info.plist" />
<PackageReference Include="AdamE.Firebase.iOS.Analytics" Version="10.29.0" />
<PackageReference Include="AdamE.Firebase.iOS.Crashlytics" Version="10.29.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-android'">
<GoogleServicesJson Include="Google-services.json" />
<PackageReference Include="Xamarin.Firebase.Analytics" Version="122.1.2" />
<PackageReference Include="Xamarin.Firebase.Crashlytics" Version="119.2.1" />
</ItemGroup>
MAUIProgram.cs
private static MauiAppBuilder RegisterFirebase(this MauiAppBuilder builder)
{
builder.ConfigureLifecycleEvents(events => {
#if IOS
events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => { Firebase.Core.App.Configure(); Firebase.Crashlytics.Crashlytics.SharedInstance.Init(); Firebase.Crashlytics.Crashlytics.SharedInstance.SetCrashlyticsCollectionEnabled(true); Firebase.Crashlytics.Crashlytics.SharedInstance.SendUnsentReports();
return false;
}));
#else
events.AddAndroid(android => android.OnCreate((activity, bundle) => { Firebase.FirebaseApp.InitializeApp(activity);Firebase.Crashlytics.FirebaseCrashlytics.Instance.SetCrashlyticsCollectionEnabled(Java.Lang.Boolean.True); Firebase.Crashlytics.FirebaseCrashlytics.Instance.SendUnsentReports();
}));
#endif
});
return builder;
}
AnalyticsService.cs
using System;
using Microsoft.Maui.Controls.Compatibility;
using Firebase.Analytics;
#if IOS
using Foundation;
#else
using Android.Content;
using Android.OS;
#endif
namespace Analytics09;
public interface IFirebaseAnalyticsService
{
void Log(string value, string eventName = "screen_view", string paramName = "screen_name");
void Log(string eventName, IDictionary<string, string> parameters);
}
public class FirebaseAnalyticsService : IFirebaseAnalyticsService
{
public void Log(string value, string eventName = "screen_view", string paramName = "screen_name")
{
Log(eventName, new Dictionary<string, string>
{
{ paramName, value.Replace("ViewModel", "", StringComparison.InvariantCulture) }
});
}
public void Log(string eventName, IDictionary<string, string> parameters)
{
#if IOS
if (parameters == null)
{
Analytics.LogEvent(eventName, (Dictionary<object, object>)null);
return;
}
var keys = new List<NSString>();
var values = new List<NSString>();
foreach (var item in parameters)
{
keys.Add(new NSString(item.Key));
values.Add(new NSString(item.Value));
}
var parametersDictionary =
NSDictionary<NSString, NSObject>.FromObjectsAndKeys(values.ToArray(), keys.ToArray(), keys.Count);
Analytics.LogEvent(eventName, parametersDictionary);
#else
var firebaseAnalytics = FirebaseAnalytics.GetInstance(Platform.CurrentActivity);
if (parameters == null)
{
firebaseAnalytics.LogEvent(eventName, null);
return;
}
var bundle = new Bundle();
foreach (var param in parameters)
{
bundle.PutString(param.Key, param.Value);
}
firebaseAnalytics.LogEvent(eventName, bundle);
#endif
}
}
MainPage.xaml.cs
private void OnEventButtonClicked(object sender, EventArgs e)
{
#if IOS
_firebaseAnalyticsService.Log("iOS_CounterButtonTapped");
#elif ANDROID
_firebaseAnalyticsService.Log("Android_CounterButtonTapped");
#endif
}
GoogleService-info.plist
<key>FIREBASE_ANALYTICS_COLLECTION_ENABLED</key>
<true></true>