Facebook Audience Network 的 Xamarin.Forms 实现
Posted
技术标签:
【中文标题】Facebook Audience Network 的 Xamarin.Forms 实现【英文标题】:Xamarin.Forms implementation of Facebook Audience Network 【发布时间】:2021-08-15 20:48:39 【问题描述】:我正在努力在我的 Xamarin.Forms 应用中实施 Facebook Audience Network。 可用的 nuget 包是旧的并且有过时的方法。
我能够自己实施广告,测试插页式广告是否正确显示,但当其他用户使用我的应用时,广告根本不显示。我在日志中看到 FAN 返回错误代码 1001 并显示“未填充”消息。
我做了什么:
添加到解决方案新的 android 类解析项目。项目包含来自 Facebook 页面的“AudienceNetwork.aar”文件。构建操作集为 LibraryProjectZip 我不得不添加一些类来修复构建错误:public partial class AdView
internal partial class IAdViewLoadConfigBuilderInvoker
IAdLoadAdConfig IAdLoadConfigBuilder.Build()
return Build();
IAdLoadConfigBuilder IAdLoadConfigBuilder.WithBid(string p0)
return WithBid(p0);
public partial class InterstitialAd
IAdLoadConfigBuilder IFullScreenAd.BuildLoadAdConfig()
return BuildLoadAdConfig();
IFullScreenAdShowConfigBuilder IFullScreenAd.BuildShowAdConfig()
return BuildShowAdConfig();
internal partial class IInterstitialAdLoadConfigBuilderInvoker
IAdLoadAdConfig IAdLoadConfigBuilder.Build()
return Build();
IAdLoadConfigBuilder IAdLoadConfigBuilder.WithBid(string p0)
return WithBid(p0);
internal partial class IInterstitialAdShowConfigBuilderInvoker
IFullScreenAdShowAdConfig IFullScreenAdShowConfigBuilder.Build()
return Build();
public abstract partial class NativeAdBase
internal partial class INativeAdLoadConfigBuilderInvoker
IAdLoadAdConfig IAdLoadConfigBuilder.Build()
return Build();
IAdLoadConfigBuilder IAdLoadConfigBuilder.WithBid(string p0)
return WithBid(p0);
public partial class RewardedVideoAd
IAdLoadConfigBuilder IFullScreenAd.BuildLoadAdConfig()
return BuildLoadAdConfig();
IFullScreenAdShowConfigBuilder IFullScreenAd.BuildShowAdConfig()
return BuildShowAdConfig();
internal partial class IRewardedVideoAdLoadConfigBuilderInvoker
IAdLoadAdConfig IAdLoadConfigBuilder.Build()
return Build();
IAdLoadConfigBuilder IAdLoadConfigBuilder.WithBid(string p0)
return WithBid(p0);
internal partial class IRewardedVideoAdShowConfigBuilderInvoker
IFullScreenAdShowAdConfig IFullScreenAdShowConfigBuilder.Build()
return Build();
public abstract partial class AdComponentFrameLayout
public void SetLayoutParams(ViewGroup.LayoutParams p0)
SetLayoutParams(p0);
public abstract partial class AdNativeComponentView
public void SetLayoutParams(ViewGroup.LayoutParams p0)
SetLayoutParams(p0);
internal partial class IInterstitialAdApiInvoker
IAdLoadConfigBuilder IFullScreenAd.BuildLoadAdConfig()
return BuildLoadAdConfig();
IFullScreenAdShowConfigBuilder IFullScreenAd.BuildShowAdConfig()
return BuildShowAdConfig();
public partial class InitSettingsBuilder
AudienceNetworkAds.IInitSettingsBuilder AudienceNetworkAds.IInitSettingsBuilder.WithInitListener(AudienceNetworkAds.IInitListener p0)
return WithInitListener(p0);
AudienceNetworkAds.IInitSettingsBuilder AudienceNetworkAds.IInitSettingsBuilder.WithMediationService(string p0)
return WithMediationService(p0);
AudienceNetworkAds.IInitSettingsBuilder AudienceNetworkAds.IInitSettingsBuilder.WithPlacementIds(IList<string> p0)
return WithPlacementIds(p0);
internal partial class IRewardedVideoAdApiInvoker
IAdLoadConfigBuilder IFullScreenAd.BuildLoadAdConfig()
return BuildLoadAdConfig();
IFullScreenAdShowConfigBuilder IFullScreenAd.BuildShowAdConfig()
return BuildShowAdConfig();
在 Android 项目的 MainActivity 中:
AudienceNetworkAds.Initialize(this);
插页式广告渲染器
public class InterstitalAdsRenderer : IInterstitalAdsService
InterstitialAd _interstitial;
public void Show(string adsId)
//AdSettings.AddTestDevice("fe9823bf-946e-4a43-b38c-f958d0bfaa31");
_interstitial = new InterstitialAd(Application.Context, adsId);
var loadAdConfig = _interstitial.BuildLoadAdConfig()
.WithAdListener(new InterstitialAdListener(_interstitial))
.Build();
_interstitial.LoadAd(loadAdConfig);
internal class InterstitialAdListener : Java.Lang.Object, IInterstitialAdListener
private readonly InterstitialAd _interstitial;
public InterstitialAdListener(InterstitialAd interstitial)
public void OnAdClicked(IAd p0)
public void OnAdLoaded(IAd p0)
_interstitial.Show();
public void OnError(IAd p0, AdError p1)
Console.WriteLine("OnError: " + p1.ErrorMessage);
public void OnLoggingImpression(IAd p0)
public void OnInterstitialDismissed(IAd p0)
public void OnInterstitialDisplayed(IAd p0)
正如我之前所说,测试设备中的测试广告(添加到测试设备的模拟器)可以正确显示广告,但在生产环境中没有广告:/
有人在 Xamarin.Forms 项目中实施了 Facebook 广告,可以告诉我如何正确执行此操作吗?
【问题讨论】:
“我必须添加一些类来修复构建错误” - 什么类?什么构建错误?您的帖子中没有代码。我们不知道你实际上在做什么。我不会花时间观看 youtube 视频来了解您在做什么。发帖前请阅读How to Ask。 @Jason 抱歉,我添加了一段负责展示广告的代码。 【参考方案1】:此错误通常表示您尚未登录 Facebook 应用程序或 Facebook 应用程序在您运行应用程序的设备中不可用
【讨论】:
以上是关于Facebook Audience Network 的 Xamarin.Forms 实现的主要内容,如果未能解决你的问题,请参考以下文章
Facebook Audience Network 导致 NullPointerException
Facebook Audience Network:需要 Facebook 登录吗?
原生 Facebook Audience Network 广告不可点击