Firebase Crashlytics 是一款轻量级的实时崩溃报告解决方案,可让您轻松管理应用中的稳定性问题。Crashlytics 会对崩溃信息进行智能分组并突出显示导致崩溃的环境因素,从而为您节省问题排查的时间。
本指南介绍了如何将 Crashlytics 集成到您的 Unity 项目,以便您记录广告响应 ID。这样当您稍后对应用中的崩溃进行问题排查时,可以查找广告响应 ID,并通过 AdMob 中的广告审核中心查找和屏蔽广告。
第 1 步:将 Firebase 添加到您的 Unity 应用
按照 Firebase Unity 集成指南中的步骤将 Firebase Crashlytics 集成到 Unity。
第 2 步:记录广告响应 ID
创建 MonoBehaviour 脚本并初始化 AdMob SDK 和 Firebase SDK。使用布尔值
isCrashlyticsInitialized
监控 Crashlytics 何时进行初始化。using GoogleMobileAds.Api; using Fabric.Crashlytics; ... public class GameObjectScript : MonoBehaviour { bool isCrashlyticsInitialized = false; public void Start() { .... // Initialize the Google Mobile Ads SDK. MobileAds.Initialize(initStatus => { }); .... // Initialize Firebase Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => { Firebase.DependencyStatus dependencyStatus = task.Result; if (dependencyStatus == Firebase.DependencyStatus.Available) { Firebase.FirebaseApp app = Firebase.FirebaseApp.DefaultInstance; isCrashlyticsInitialized = true; } else { UnityEngine.Debug.LogError(System.String.Format( "Could not resolve all Firebase dependencies: {0}",dependencyStatus)); // Firebase Unity SDK is not safe to use here. } } } }
请求横幅广告。
using GoogleMobileAds.Api; using Fabric.Crashlytics; ... public class GameObjectScript : MonoBehaviour { public void Start() { ... // Initialize the Google Mobile Ads SDK. MobileAds.Initialize(initStatus => { }); // Initialize Firebase. Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => { Firebase.DependencyStatus dependencyStatus = task.Result; if (dependencyStatus == Firebase.DependencyStatus.Available) { // Create and hold a reference to your FirebaseApp, // where app is a Firebase.FirebaseApp property of your // application class. // Crashlytics will use the DefaultInstance, as well; // this ensures that Crashlytics is initialized. Firebase.FirebaseApp app = Firebase.FirebaseApp.DefaultInstance; isCrashlyticsInitialized = true; } else { UnityEngine.Debug.LogError(System.String.Format( "Could not resolve all Firebase dependencies: {0}",dependencyStatus)); // Firebase Unity SDK is not safe to use here. } }); // Request Banner View. this.RequestBanner(); ... } public void RequestBanner() { #if UNITY_ANDROID string adUnitId = "ca-app-pub-3940256099942544/6300978111"; #elif UNITY_IPHONE string adUnitId = "ca-app-pub-1220882738324941/1255739139"; #else string adUnitId = "unexpected_platform"; #endif // Create a 320x50 banner at the top of the screen. this.bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom); AdRequest request = new AdRequest(); this.bannerView.LoadAd(request); // Called when an ad request has successfully loaded. this.bannerView.OnAdLoaded += this.HandleOnAdLoaded; } }
获取
ResponseInfo
对象OnAdLoaded
并将响应 ID 记录到 Crashlytics。public void HandleOnAdLoaded(object sender, EventArgs args) { ResponseInfo responseInfo = this.bannerView.GetResponseInfo(); if (responseInfo != null) { String adResponseId = responseInfo.GetResponseId(); // Log to Crashlytics. if (isCrashlyticsInitialized) { Crashlytics.SetCustomKey("banner_ad_response_id", adResponseId); } } }
大功告成!现在,您可以在 Crashlytics 信息中心内“崩溃”会话的“键”部分看到最新的 banner_ad_response_id
。请注意,某些键最多可能需要 4 小时才能显示在信息中心上。