開始使用

Google User Messaging Platform (UMP) SDK 是一項隱私權與訊息工具, 協助您管理隱私權選擇若需更多資訊,請參閲 關於隱私權與訊息

建立訊息類型

使用以下其中一則文字建立使用者訊息: 可用的使用者訊息類型 中,隱私權與訊息分頁 廣告管理員 讓他們使用服務帳戶UMP SDK 會嘗試顯示 從應用程式 ID Ad Manager 建立的隱私權訊息 您在專案中設定的內容

詳情請參閱 關於隱私權與訊息

新增應用程式 ID

您可以在「 Ad Manager 使用者介面。 將身分證件新增至 替換為下列程式碼片段:

在每個應用程式中,應要求使用者更新同意聲明資訊 使用 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. 如果隱私權選項進入點 您必須在應用程式中加入可見且可互動的 UI 元素。
  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 可讓您測試應用程式行為,就像測試裝置 地址在歐洲經濟區或英國境內,並使用 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 的狀態,方便您模擬使用者的首次安裝體驗。 但 SDK 提供的 reset() 方法可以執行這項操作。

ConsentInformation.instance.reset();

GitHub 上的範例