You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Dotnet Maui移动应用中配置Google.UserMessagingPlatform实现GRDP Admod?

.NET Maui 配置 Google User Messaging Platform(GDPR 合规 AdMob 设置)

当然有开发者完成过这项配置,以下是经过验证的实操步骤:

一、安装依赖包

  • 安装适配.NET Maui的Google.UserMessagingPlatform绑定包(目前可使用Xamarin.Google.UserMessagingPlatform,确保选择兼容.NET 6+的版本)
  • 同步安装Google.MobileAds包,保证AdMob基础功能可用

二、平台专属配置

Android 端

  1. 编辑Platforms/Android/AndroidManifest.xml,添加必要权限与元数据:
<!-- 基础网络权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- AdMob应用ID,替换为你的实际ID -->
<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
<!-- 延迟测量初始化,配合UMP流程 -->
<meta-data
    android:name="com.google.android.gms.ads.DELAY_APP_MEASUREMENT_INIT"
    android:value="true"/>
  1. 在MainActivity.cs中初始化UMP并处理 consent 流程:
protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    UserMessagingPlatform.Initialize(this);
    LoadConsentForm();
}

private void LoadConsentForm()
{
    var consentParams = new ConsentRequestParameters.Builder()
        .SetTagForUnderAgeOfConsent(false) // 根据你的应用受众设置
        .Build();

    UserMessagingPlatform.LoadConsentForm(this, consentParams, (form, loadError) =>
    {
        if (loadError != null)
        {
            // 加载失败,直接初始化AdMob(根据业务逻辑调整)
            InitializeAdMob();
            return;
        }

        UserMessagingPlatform.GetConsentInformation(this).GetConsentStatus((status, reqError) =>
        {
            if (status == ConsentStatus.Required)
            {
                // 需要用户授权,弹出表单
                form.Show(this, showError =>
                {
                    InitializeAdMob();
                });
            }
            else
            {
                // 已授权或无需授权,直接初始化AdMob
                InitializeAdMob();
            }
        });
    });
}

private void InitializeAdMob()
{
    MobileAds.Initialize(this);
}

iOS 端

  1. 编辑Platforms/iOS/Info.plist,添加配置项:
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy</string> <!-- 替换为你的AdMob应用ID -->
<key>SKAdNetworkItems</key>
<array>
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>cstr6suwn9.skadnetwork</string> <!-- Google SKAdNetwork ID -->
    </dict>
</array>
  1. 在AppDelegate.cs中处理UMP初始化:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    UserMessagingPlatform.SharedInstance.Initialize();
    LoadConsentForm();
    return base.FinishedLaunching(app, options);
}

private void LoadConsentForm()
{
    var consentParams = new ConsentRequestParameters
    {
        TagForUnderAgeOfConsent = false
    };

    UserMessagingPlatform.SharedInstance.LoadConsentForm(consentParams, (form, loadError) =>
    {
        if (loadError != null)
        {
            InitializeAdMob();
            return;
        }

        UserMessagingPlatform.SharedInstance.ConsentInformation.GetConsentStatus((status, reqError) =>
        {
            if (status == ConsentStatus.Required)
            {
                form.PresentFromViewController(Window.RootViewController, presentError =>
                {
                    InitializeAdMob();
                });
            }
            else
            {
                InitializeAdMob();
            }
        });
    });
}

private void InitializeAdMob()
{
    MobileAds.SharedInstance.Start(completionHandler: null);
}

三、测试要点

  • 使用Google提供的测试设备ID进行测试,避免触发真实广告与合规风险
  • 切换到欧盟地区的网络环境,验证 consent 表单是否正常弹出
  • 测试用户选择不同授权选项后,AdMob广告的加载状态是否符合预期

内容的提问来源于stack exchange,提问作者Ma Việt Tùng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 16:58:11