开始使用

Google User Messaging Platform (UMP) SDK 是一种隐私权和消息工具,用于 可帮助您管理隐私选项。如需了解详情,请参阅 关于隐私权和消息

创建消息类型

使用以下任一方式创建用户消息: 可用的用户消息类型 隐私权和消息功能标签页 Ad Manager 。UMP SDK 会尝试显示 使用 Ad Manager 应用 ID 创建的隐私权消息 。

有关详情,请参阅 隐私权和消息简介

添加应用 ID

您可以在 Ad Manager 界面。 将此 ID 添加到您的 替换为以下代码段:

您应该在每个应用中请求更新用户的用户意见征求信息 使用 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 privacy message form.
    },
    (FormError error) {
      // Handle the error.
    },
  );
}

根据需要加载并显示隐私权消息表单

获得最新的意见征求状态后,请调用 loadAndShowConsentFormIfRequired() 加载 征求用户意见。加载完成后,表单会立即显示。

@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 。

隐私设置选项

部分隐私权消息表单由发布商呈现的隐私权呈现 选项入口点,让用户能够随时管理自己的隐私选项。 如需详细了解用户会看到哪些消息,请前往隐私权选项 入口点,请参阅 可用的用户消息类型

如需实现隐私选项入口点,请完成以下步骤:

  1. 请查看 getPrivacyOptionsRequirementStatus()
  2. 如果隐私选项入口点 您需要向应用添加一个可见且可互动的界面元素。
  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))
                  ],
                ));
          })
    ];
  }
}

提出广告请求

在您的应用中发出广告请求之前,请检查您是否已征得用户同意 使用 canRequestAds()。有两个 在征求用户意见时应检查以下事项:

  • 在当前会话中征求用户意见后。
  • 致电 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.
    }
  }
}

测试

如果您想在开发过程中测试应用中的集成情况,请按照 这些步骤以程序化方式注册测试设备。请务必移除 代码,在您发布应用之前设置这些测试设备 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 提供了一种方法来测试应用行为,就好像设备被 位于欧洲经济区 (EEA) 或英国境内的用户使用 the DebugGeography field on ConsentDebugSettings。请注意, 调试设置仅适用于测试设备。

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

ConsentRequestParameters params =
    ConsentRequestParameters(consentDebugSettings: debugSettings);

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

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

ConsentInformation.instance.reset();

GitHub 上的示例