开始使用

根据 Google 欧盟地区用户意见征求政策,您必须向位于欧洲经济区 (EEA) 以及英国境内的用户披露相关信息,在法律有相应要求的情况下,必须征得他们的同意才能使用 Cookie 或其他本地存储方式,以及使用个人数据(例如 AdID)来投放广告。此政策反映了欧盟《电子隐私指令》和《一般数据保护条例》(GDPR) 的要求。

为了帮助发布商履行此政策规定的职责,Google 提供了 User Messaging Platform (UMP) SDK。UMP SDK 已更新,可支持最新的 IAB 标准。现在,所有这些配置都可以在 AdMob 隐私权和消息中方便地处理。

前提条件

创建消息类型

在您的 AdMob 账号的隐私权和消息标签页下,使用 可用的用户消息类型 创建用户消息。UMP SDK 会尝试显示通过您项目中设置的 AdMob 应用 ID 创建的用户消息。如果没有为您的应用配置消息,SDK 将返回错误。

如需了解详情,请参阅 隐私权和消息简介

您应在每次启动应用时使用 requestConsentInfoUpdate()请求更新用户的用户意见征求信息。这决定了您的用户是否需要提供同意声明(如果尚未提供)或同意情况已过期。

以下示例展示了如何在应用启动时检查状态:

@override
void initState() {
  super.initState();

  // Create a ConsentRequestParameters object.
  final params = ConsentRequestParameters();

  // Request an update for the consent information.
  ConsentInformation.instance.requestConsentInfoUpdate(
    params,
    () async {
      // TODO: Load and present the consent form.
    },
    (FormError error) {
      // Handle the error.
    },
  );
}

根据需要加载并显示用户意见征求表单

收到最新的同意情况后,请对ConsentForm 类调用loadAndShowConsentFormIfRequired() 以加载用户意见征求表单。如果需要用户意见状态,SDK 会加载一个表单,并 通过提供的 立即显示该表单。在用户关闭表单后,系统会调用 callback 。如果不需要征得用户同意,系统会立即 callback 。

@override
void initState() {
  super.initState();

  // Create a ConsentRequestParameters object.
  final params = ConsentRequestParameters();

  // Request an update for the consent information.
  ConsentInformation.instance.requestConsentInfoUpdate(
    params,
    () async {
      ConsentForm.loadAndShowConsentFormIfRequired((loadAndShowError) {
        if (loadAndShowError != null) {
          // Consent gathering failed.
        }

        // Consent has been gathered.
      });
    },
    (FormError error) {
      // Handle the error.
    },
  );
}

如果您需要在用户做出选择或关闭表单后执行任何操作,请将相应逻辑放在表单的 callback中。

提出广告请求

在您的应用中请求广告之前,请检查您是否已使用 canRequestAds()征得用户同意。在征求用户意见时,有两个位置可供检查:

  1. 在当前会话中收集用户意见后。
  2. 调用 requestConsentInfoUpdate()后。可能已在上一次会话中征得用户同意。为缩短延迟时间,我们建议不要等待回调完成,以便在应用启动后尽快开始加载广告。

如果在征求用户意见的过程中出现错误,您仍应尝试请求广告。UMP SDK 会使用上一会话中的用户同意情况。

class AppExampleState extends State<AppExample> {

  // Use a bool to initialize the Mobile Ads SDK and load ads once.
  var _isMobileAdsInitializeCalled = false;

  @override
  void initState() {
    super.initState();

    // Create a ConsentRequestParameters object.
    final params = ConsentRequestParameters();

    // Request an update for the consent information.
    ConsentInformation.instance.requestConsentInfoUpdate(
      params,
      () async {
        ConsentForm.loadAndShowConsentFormIfRequired((loadAndShowError) {
          if (loadAndShowError != null) {
            // Consent gathering failed.
          }

          // Consent has been gathered.
          _initializeMobileAdsSDK();
        });
      },
      (FormError error) {
        // Handle the error.
      },
    );

    // Check if you can initialize the Mobile Ads SDK in parallel while
    // checking for new consent information. Consent obtained in the
    // previous session can be used to request ads.
    _initializeMobileAdsSDK();
  }

  void _initializeMobileAdsSDK() async {
    if (_isMobileAdsInitializeCalled) {
      return;
    }

    // Initialize the Mobile Ads SDK if the SDK has gathered consent aligned with
    // the app's configured messages.
    var canRequestAds = await ConsentInformation.instance.canRequestAds();
    if (canRequestAds) {
      setState(() {
        _isMobileAdsInitializeCalled = true;
      });

      // Initialize the Mobile Ads SDK.
      MobileAds.instance.initialize();

      // TODO: Request an ad.
    }
  }
}

隐私设置选项

有些用户意见征求表单会要求用户随时修改同意情况。请根据需要按照以下步骤实现隐私权选项按钮。

要实现这一目标,需要完成以下步骤:

  1. 实现可触发隐私权选项表单的界面元素,例如应用的设置页面中的按钮。
  2. loadAndShowConsentFormIfRequired() 完成后,请检查getPrivacyOptionsRequirementStatus() 以确定是否显示可呈现隐私权选项表单的界面元素。
  3. 当用户与您的界面元素交互时,调用showPrivacyOptionsForm() 以显示该表单,以便用户能够随时更新其隐私选项。
class AppExampleState extends State<AppExample> {
  static const _privacySettingsText = 'Privacy Settings';

  // Use a bool to initialize the Mobile Ads SDK and load ads once.
  var _isMobileAdsInitializeCalled = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App Example',
      home: Scaffold(
          appBar: AppBar(
            title: const Text('App Example'),
            actions: _isMobileAdsSDKInitialized
                // Regenerate the options menu to include a privacy setting.
                ? _privacySettingsAppBarAction()
                : null
          ),
          body: // ...
      ),
    );
  }

  List<Widget> _privacySettingsAppBarAction() {
    return <Widget>[
      FutureBuilder(
          future: ConsentInformation.instance.isPrivacyOptionsRequired(),
          builder: (context, snapshot) {
            final bool visibility = snapshot.data ?? false;
            return Visibility(
                visible: visibility,
                child: PopupMenuButton<String>(
                  onSelected: (String result) {
                    if (result == _privacySettingsText) {
                      ConsentForm.showPrivacyOptionsForm((formError) {
                        if (formError != null) {
                          debugPrint(
                              "${formError.errorCode}: ${formError.message}");
                        }
                      });
                    }
                  },
                  itemBuilder: (BuildContext context) =>
                      <PopupMenuEntry<String>>[
                    const PopupMenuItem<String>(
                        value: _privacySettingsText,
                        child: Text(_privacySettingsText))
                  ],
                ));
          })
    ];
  }
}

测试

如果您希望在开发过程中测试应用中的集成,请按照以下步骤以编程方式注册测试设备。在发布应用之前,请务必移除设置这些测试设备 ID 的代码。

  1. 致电 requestConsentInfoUpdate()
  2. 检查日志输出中是否有类似于以下示例的消息,该消息显示了您的设备 ID 以及如何将其添加为测试设备:

    Android

    Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("33BE2250B43518CCDA7DE426D04EE231")
    to set this as a debug device.
    

    iOS

    <UMP SDK>To enable debug mode for this device,
    set: UMPDebugSettings.testDeviceIdentifiers = @[2077ef9a63d2b398840261c8221a0c9b]
    
  3. 将测试设备 ID 复制到剪贴板。

  4. 修改代码,将 调用 ConsentDebugSettings.testIdentifiers 并传入 您的测试设备 ID 列表。

    ConsentDebugSettings debugSettings = ConsentDebugSettings(
      testIdentifiers: ["TEST-DEVICE-HASHED-ID"],
    );
    
    ConsentRequestParameters params =
        ConsentRequestParameters(consentDebugSettings: debugSettings);
    
    ConsentInformation.instance.requestConsentInfoUpdate(params, () async {
      // ...
    };
    

强制指定地理位置

UMP SDK 提供了一种方法,可让您使用 the DebugGeography field on ConsentDebugSettings测试应用的行为,就如同设备位于欧洲经济区 (EEA) 或英国一样。请注意,调试设置仅适用于测试设备。

ConsentDebugSettings debugSettings = ConsentDebugSettings(
  debugGeography: DebugGeography.debugGeographyEea,
  testIdentifiers: ["TEST-DEVICE-HASHED-ID"],
);

ConsentRequestParameters params =
    ConsentRequestParameters(consentDebugSettings: debugSettings);

ConsentInformation.instance.requestConsentInfoUpdate(params, () async {
  // ...
};

使用 UMP SDK 测试应用时,您可能会发现重置 SDK 的状态很有用,这样您就可以模拟用户的首次安装体验。为此,SDK 提供了 reset() 方法。

ConsentInformation.instance.reset();