Google User Messaging Platform(UMP)SDK は、プライバシーに関するユーザー設定の管理に役立つプライバシーとメッセージに関するツールです。詳しくは、プライバシーとメッセージについてをご覧ください。
前提条件
- Android API レベル 21 以降
メッセージ タイプを作成する
AdMob アカウントの [プライバシーとメッセージ] タブで、利用可能なユーザー向けメッセージの種類のいずれかを使用してユーザー向けメッセージを作成していること。UMP SDK は、プロジェクトで設定された AdMob アプリケーション ID から作成されたプライバシー メッセージを表示しようとします。
詳しくは、プライバシーとメッセージについてをご覧ください。
Gradle でインストールする
Google User Messaging Platform SDK の依存関係をモジュールのアプリレベルの Gradle ファイル(通常は app/build.gradle)に追加します。
dependencies {
  implementation("com.google.android.ump:user-messaging-platform:3.1.0")
}
アプリの build.gradle を変更したら、プロジェクトを Gradle ファイルと必ず同期してください。
アプリケーション ID を追加する
アプリケーション ID は AdMob の管理画面で確認できます。次のコード スニペットを使用して、ID を AndroidManifest.xml に追加します。
<manifest>
  <application>
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
  </application>
</manifest>
ユーザーの同意情報を取得する
ユーザーの同意情報を取得するには、次の操作を行います。
ConsentInformation のインスタンスを宣言します。
Java
private final ConsentInformation consentInformation;
Kotlin
private lateinit val consentInformation: ConsentInformation
ConsentInformation インスタンスを初期化します。
Java
consentInformation = UserMessagingPlatform.getConsentInformation(context);
Kotlin
consentInformation = UserMessagingPlatform.getConsentInformation(context)
requestConsentInfoUpdate() を使用して、アプリの起動ごとにユーザーの同意情報の更新をリクエストする必要があります。このリクエストは、次のことを確認します。
- 同意が必要かどうか。たとえば、同意が初めて必要になった場合や、以前の同意の決定の有効期限が切れた場合などです。
- プライバシー オプションのエントリ ポイントが必要かどうか。一部のプライバシー メッセージでは、アプリでユーザーがいつでもプライバシー オプションを変更できるようにする必要があります。
Java
// Requesting an update to consent information should be called on every app launch.
consentInformation.requestConsentInfoUpdate(
    activity,
    params,
    () -> // Called when consent information is successfully updated.
    requestConsentError -> // Called when there's an error updating consent information.
Kotlin
// Requesting an update to consent information should be called on every app launch.
consentInformation.requestConsentInfoUpdate(
  activity,
  params,
  {
    // Called when consent information is successfully updated.
  },
  { requestConsentError ->
    // Called when there's an error updating consent information.
  },
)
プライバシー メッセージ フォームを読み込んで表示する
最新の同意ステータスを取得したら、
loadAndShowConsentFormIfRequired() を呼び出して、ユーザーの同意を得るために必要なフォームを読み込みます。読み込み後、フォームがすぐに表示されます。
Java
UserMessagingPlatform.loadAndShowConsentFormIfRequired(
    activity,
    formError -> {
      // Consent gathering process is complete.
    });
Kotlin
UserMessagingPlatform.loadAndShowConsentFormIfRequired(activity) { formError ->
  // Consent gathering process is complete.
}
プライバシー オプション
一部のプライバシー メッセージ フォームは、パブリッシャーがレンダリングしたプライバシー オプションのエントリ ポイントから表示され、ユーザーはいつでもプライバシー オプションを管理できます。プライバシー オプションのエントリ ポイントでユーザーに表示されるメッセージについて詳しくは、利用可能なユーザー メッセージ タイプをご覧ください。
プライバシー オプションのエントリ ポイントが必要かどうかを確認する
requestConsentInfoUpdate() を呼び出した後、
getPrivacyOptionsRequirementStatus() をチェックして、アプリにプライバシー オプションのエントリ ポイントが必要かどうかを判断します。エントリ ポイントが必要な場合は、プライバシー オプション フォームを表示する、表示可能で操作可能な UI 要素をアプリに追加します。プライバシー エントリ ポイントが不要な場合は、UI 要素が非表示で操作可能になるように構成します。
Java
/** Helper variable to determine if the privacy options form is required. */
public boolean isPrivacyOptionsRequired() {
  return consentInformation.getPrivacyOptionsRequirementStatus()
      == PrivacyOptionsRequirementStatus.REQUIRED;
}
Kotlin
/** Helper variable to determine if the privacy options form is required. */
val isPrivacyOptionsRequired: Boolean
  get() =
    consentInformation.privacyOptionsRequirementStatus ==
      ConsentInformation.PrivacyOptionsRequirementStatus.REQUIRED
プライバシー オプションの要件ステータスの完全なリストについては、
ConsentInformation.PrivacyOptionsRequirementStatus をご覧ください。
プライバシー オプション フォームを表示する
ユーザーが要素を操作したときに、プライバシー オプションのフォームを表示します。
Java
UserMessagingPlatform.showPrivacyOptionsForm(activity, onConsentFormDismissedListener);
Kotlin
UserMessagingPlatform.showPrivacyOptionsForm(activity, onConsentFormDismissedListener)
ユーザーの同意を得て広告をリクエストする
広告をリクエストする前に、
canRequestAds() を使用してユーザーの同意を得ているかどうかを確認します。
Java
consentInformation.canRequestAds();
Kotlin
consentInformation.canRequestAds()
同意を取得しながら広告をリクエストできるかどうかを確認する場所は次のとおりです。
- UMP SDK が現在のセッションで同意を収集した後。
- requestConsentInfoUpdate()を呼び出した直後。UMP SDK は、前のアプリ セッションで同意を取得している可能性があります。
同意取得プロセス中にエラーが発生した場合は、広告をリクエストできるかどうかを確認します。UMP SDK は、前回のアプリ セッションの同意ステータスを使用します。
重複する広告リクエストの作業を防ぐ
同意を取得した後、および 
requestConsentInfoUpdate() を呼び出した後に 
canRequestAds() を確認する際は、両方のチェックで true が返される可能性のある冗長な広告リクエストをロジックで防ぐようにしてください。たとえば、ブール変数を使用します。
テスト
開発中のアプリで統合をテストする場合は、次の手順に沿ってプログラムでテストデバイスを登録します。アプリをリリースする前に、テストデバイス ID を設定するコードを必ず削除してください。
- requestConsentInfoUpdate()までお電話ください。
- ログ出力で次のようなメッセージを確認します。ここには、デバイス ID とテストデバイスとしてデバイスを追加する方法が示されています。 - Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("33BE2250B43518CCDA7DE426D04EE231") to set this as a debug device.
- テストデバイスの ID をクリップボードにコピーします。 
- コードを変更して - ConsentDebugSettings.Builder().TestDeviceHashedIdsを呼び出し、テストデバイス ID のリストを渡します。- Java- ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(this) .addTestDeviceHashedId("TEST-DEVICE-HASHED-ID") .build(); ConsentRequestParameters params = new ConsentRequestParameters .Builder() .setConsentDebugSettings(debugSettings) .build(); consentInformation = UserMessagingPlatform.getConsentInformation(this); // Include the ConsentRequestParameters in your consent request. consentInformation.requestConsentInfoUpdate( this, params, // ... );- Kotlin- val debugSettings = ConsentDebugSettings.Builder(this) .addTestDeviceHashedId("TEST-DEVICE-HASHED-ID") .build() val params = ConsentRequestParameters .Builder() .setConsentDebugSettings(debugSettings) .build() consentInformation = UserMessagingPlatform.getConsentInformation(this) // Include the ConsentRequestParameters in your consent request. consentInformation.requestConsentInfoUpdate( this, params, // ... )
地域を強制的に適用する
UMP SDK では、setDebugGeography() を使用して、デバイスが EEA や英国などのさまざまな地域にあるかのようにアプリの動作をテストできます。デバッグ設定はテスト用デバイスでのみ機能します。
Java
ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(this)
    .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
    .addTestDeviceHashedId("TEST-DEVICE-HASHED-ID")
    .build();
ConsentRequestParameters params = new ConsentRequestParameters
    .Builder()
    .setConsentDebugSettings(debugSettings)
    .build();
consentInformation = UserMessagingPlatform.getConsentInformation(this);
// Include the ConsentRequestParameters in your consent request.
consentInformation.requestConsentInfoUpdate(
    this,
    params,
    ...
);
Kotlin
val debugSettings = ConsentDebugSettings.Builder(this)
    .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
    .addTestDeviceHashedId("TEST-DEVICE-HASHED-ID")
    .build()
val params = ConsentRequestParameters
    .Builder()
    .setConsentDebugSettings(debugSettings)
    .build()
consentInformation = UserMessagingPlatform.getConsentInformation(this)
// Include the ConsentRequestParameters in your consent request.
consentInformation.requestConsentInfoUpdate(
    this,
    params,
    ...
)
同意ステータスをリセットする
UMP SDK でアプリをテストする際、ユーザーの初回インストール エクスペリエンスをシミュレーションできるように、SDK の状態をリセットすると便利な場合があります。SDK には、これを行う reset() メソッドが用意されています。
Java
consentInformation.reset();
Kotlin
consentInformation.reset()
GitHub の例
このページで説明する UMP SDK 統合例の全文については、Java BannerExample と Kotlin BannerExample をご覧ください。