Rozpocznij

Zgodnie z polityką Google w zakresie zgody użytkownika z UE musisz udzielać odpowiednich informacji użytkownikom z Europejskiego Obszaru Gospodarczego (EOG) i Wielkiej Brytanii oraz uzyskiwać ich zgodę na stosowanie plików cookie lub innych środków do lokalnego przechowywania danych, jeśli jest to wymagane prawnie. Musisz też uzyskać ich zgodę na wykorzystywanie danych osobowych (np. AdID) do wyświetlania reklam. Polityka ta odzwierciedla wymagania UE zawarte w dyrektywie o prywatności i łączności elektronicznej oraz w Ogólnym rozporządzeniu o ochronie danych (RODO).

Aby pomóc wydawcom w wypełnieniu obowiązków, jakie nakłada na nich ta polityka, Google oferuje pakiet SDK User Messaging Platform (UMP). Zaktualizowaliśmy pakiet UMP SDK, aby obsługiwał najnowsze standardy IAB. Wszystkie te ustawienia można teraz wygodnie obsługiwać AdMob w sekcji Prywatność i wyświetlanie wiadomości.

Wymagania wstępne

Typy wiadomości dla użytkowników

W sekcji Typy wiadomości dla użytkowników znajdziesz pełną listę obsługiwanych wiadomości. Szczegółowe instrukcje implementacji każdego typu wiadomości znajdziesz w panelu nawigacyjnym po lewej stronie.

Instalowanie za pomocą narzędzia Gradle

Jeśli używasz pakietu SDK do reklam mobilnych Google w wersji 19.8.0 lub nowszej, możesz pominąć ten krok instalacji Gradle. W przeciwnym razie uwzględnij pakiet UMP SDK w build.gradle Twojej aplikacji w ten sposób:

dependencies {
    // This dependency is automatically included by Google Mobile Ads SDK 19.8.0
    // or higher.
    implementation 'com.google.android.ump:user-messaging-platform:2.0.0'
}

Po wprowadzeniu zmian w build.gradle aplikacji zsynchronizuj swój projekt z plikami Gradle.

Zaktualizuj plik manifestu

Następnie znajdź identyfikator aplikacji i dodaj do AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rewardedinterstitialexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <!-- Sample app ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Określ, czy wiadomość musi być wyświetlana

Przy każdym uruchomieniu aplikacji należy poprosić o aktualizację informacji z prośbą o zgodę użytkownika. Aby to zrobić, requestConsentInfoUpdate() załaduj formularz. Ta opcja określa, czy użytkownik musi wyrazić zgodę, jeśli jeszcze tego nie zrobił lub czy jego ważność wygasła.

Jeśli to konieczne, prezentuj formularz, korzystając z informacji przechowywanych w obiekcie consentInformation.

Oto przykład sprawdzania stanu przy uruchamianiu aplikacji:

Java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.google.android.ump.ConsentForm;
import com.google.android.ump.ConsentInformation;
import com.google.android.ump.ConsentRequestParameters;
import com.google.android.ump.FormError;
import com.google.android.ump.UserMessagingPlatform;

public class MainActivity extends AppCompatActivity {
  private ConsentInformation consentInformation;
  private ConsentForm consentForm;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set tag for under age of consent. false means users are not under
    // age.
    ConsentRequestParameters params = new ConsentRequestParameters
        .Builder()
        .setTagForUnderAgeOfConsent(false)
        .build();

    consentInformation = UserMessagingPlatform.getConsentInformation(this);
    consentInformation.requestConsentInfoUpdate(
        this,
        params,
        new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
            @Override
            public void onConsentInfoUpdateSuccess() {
                // The consent information state was updated.
                // You are now ready to check if a form is available.
            }
        },
        new ConsentInformation.OnConsentInfoUpdateFailureListener() {
            @Override
            public void onConsentInfoUpdateFailure(FormError formError) {
                // Handle the error.
            }
        });
  }
}

Kotlin

package com.example.myapplication

import com.google.android.ump.ConsentForm
import com.google.android.ump.ConsentInformation
import com.google.android.ump.ConsentInformation.OnConsentInfoUpdateFailureListener
import com.google.android.ump.ConsentInformation.OnConsentInfoUpdateSuccessListener
import com.google.android.ump.ConsentRequestParameters
import com.google.android.ump.UserMessagingPlatform

class MainActivity : AppCompatActivity() {
  private lateinit var consentInformation: ConsentInformation
  private lateinit var consentForm: ConsentForm

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // Set tag for under age of consent. false means users are not under
    // age.
    val params = ConsentRequestParameters
        .Builder()
        .setTagForUnderAgeOfConsent(false)
        .build()

    consentInformation = UserMessagingPlatform.getConsentInformation(this)
    consentInformation.requestConsentInfoUpdate(
        this,
        params,
        OnConsentInfoUpdateSuccessListener {
          // The consent information state was updated.
          // You are now ready to check if a form is available.
        },
        OnConsentInfoUpdateFailureListener {
          // Handle the error.
        })
  }
}

Wczytywanie formularza (jeśli jest dostępny)

Przed wyświetleniem formularza musisz określić, czy jest on dostępny. Niedostępne formularze mogą być spowodowane tym, że użytkownik włączył ograniczone śledzenie reklam lub zostały przez nie otagowane.

Aby sprawdzić dostępność formularza, użyj funkcjithe isConsentFormAvailable() method on the ConsentInformation instance utworzonej wcześniej.

Następnie dodaj metodę kodu, aby wczytać formularz:

Java

...
    consentInformation.requestConsentInfoUpdate(
        this,
        params,
        new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
          @Override
          public void onConsentInfoUpdateSuccess() {
            // The consent information state was updated.
            // You are now ready to check if a form is available.
            if (consentInformation.isConsentFormAvailable()) {
              loadForm();
            }
          }
        },
        new ConsentInformation.OnConsentInfoUpdateFailureListener() {
          @Override
          public void onConsentInfoUpdateFailure(FormError formError) {
            // Handle the error.
          }
    });
  }

  public void loadForm() {

  }
}

Kotlin

...
    consentInformation.requestConsentInfoUpdate(
        this,
        params,
        OnConsentInfoUpdateSuccessListener {
          // The consent information state was updated.
          // You are now ready to check if a form is available.
          if (consentInformation.isConsentFormAvailable) {
            loadForm()
          }
        },
        OnConsentInfoUpdateFailureListener {
          // Handle the error.
        })
  }

  fun loadForm() {

  }
}

Aby wczytać formularz, użyj the static loadConsentForm() method on the UserMessagingPlatform class.

Java

public void loadForm() {
  // Loads a consent form. Must be called on the main thread.
  UserMessagingPlatform.loadConsentForm(
      this,
      new UserMessagingPlatform.OnConsentFormLoadSuccessListener() {
        @Override
        public void onConsentFormLoadSuccess(ConsentForm consentForm) {
          MainActivity.this.consentForm = consentForm;
        }
      },
      new UserMessagingPlatform.OnConsentFormLoadFailureListener() {
        @Override
        public void onConsentFormLoadFailure(FormError formError) {
          // Handle the error.
        }
      }
  );
}

Kotlin

fun loadForm() {
  // Loads a consent form. Must be called on the main thread.
  UserMessagingPlatform.loadConsentForm(
      this,
      UserMessagingPlatform.OnConsentFormLoadSuccessListener {
        this.consentForm = consentForm
      },
      UserMessagingPlatform.OnConsentFormLoadFailureListener {
        // Handle the error.
      }
  )
}

W razie potrzeby pokaż formularz

Po sprawdzeniu dostępności i załadowaniu formularza skorzystaj z metodyshow() w instancjiConsentForm , aby zaprezentować formularz.

Użyj obiektuconsentInformation z wcześniejszego użycia, aby sprawdzićconsent status i zaktualizować metodęloadForm() :

Java

public void loadForm() {
  // Loads a consent form. Must be called on the main thread.
  UserMessagingPlatform.loadConsentForm(
      this,
      new UserMessagingPlatform.OnConsentFormLoadSuccessListener() {
        @Override
        public void onConsentFormLoadSuccess(ConsentForm consentForm) {
          MainActivity.this.consentForm = consentForm;
          if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.REQUIRED) {
            consentForm.show(
                MainActivity.this,
                new ConsentForm.OnConsentFormDismissedListener() {
                  @Override
                  public void onConsentFormDismissed(@Nullable FormError formError) {
                    if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.OBTAINED) {
                      // App can start requesting ads.
                    }

                    // Handle dismissal by reloading form.
                    loadForm();
                  }
            });
          }
        }
      },
      new UserMessagingPlatform.OnConsentFormLoadFailureListener() {
        @Override
        public void onConsentFormLoadFailure(FormError formError) {
          // Handle Error.
        }
      }
  );
}

Kotlin

fun loadForm() {
  // Loads a consent form. Must be called on the main thread.
  UserMessagingPlatform.loadConsentForm(
      this,
      UserMessagingPlatform.OnConsentFormLoadSuccessListener {
        this.consentForm = consentForm
        if (consentInformation.consentStatus == ConsentInformation.ConsentStatus.REQUIRED) {
          consentForm.show(
              this,
              ConsentForm.OnConsentFormDismissedListener {
                if (consentInformation.consentStatus == ConsentInformation.ConsentStatus.OBTAINED) {
                  // App can start requesting ads.
                }

                // Handle dismissal by reloading form.
                loadForm()
              }
          )
        }
      },
      UserMessagingPlatform.OnConsentFormLoadFailureListener {
        // Handle the error.
      }
  )
}

Jeśli chcesz coś zrobić po dokonaniu wyboru lub zamknięciu formularza, umieść tę logikę w module obsługi wypełniania lub wywołania zwrotnego formularza.

Testowanie

Wymuś położenie geograficzne

Pakiet UMP SDK umożliwia przetestowanie działania aplikacji tak, jakby urządzenie znajdowało się w Europejskim Obszarze Gospodarczym lub Wielkiej Brytanii za pomocą the setDebugGeography method on ConsentDebugSettings.Builder.

Aby użyć funkcji debugowania, musisz podać zaszyfrowany identyfikator urządzenia testowego w ustawieniach debugowania aplikacji. Jeśli wywołujesz funkcjęrequestConsentInfoUpdate() bez ustawienia tej wartości, aplikacja rejestruje wymagany skrót identyfikatora po uruchomieniu.

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);
consentInformation.requestConsentInfoUpdate(this,
    params,
    new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
      @Override
      public void onConsentInfoUpdateSuccess() {
        // The consent information state was updated.
        // You are now ready to check if a form is available.
      }
    },
    new ConsentInformation.OnConsentInfoUpdateFailureListener() {
      @Override
      public void onConsentInfoUpdateFailure(FormError formError) {
        // Handle the error.
      }
});

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)
    consentInformation.requestConsentInfoUpdate(
        this,
        params,
        OnConsentInfoUpdateSuccessListener {
          // The consent information state was updated.
          // You are now ready to check if a form is available.
        },
        OnConsentInfoUpdateFailureListener {
          // Handle the error.
        })
}

W narzędziu DebugGeographymożesz wymusić położenie geograficzne na 1 z tych sposobów:

Debugowanie geograficzne Opis
DEBUG_GEOGRAPHY_DISABLED Debugowanie geograficzne jest wyłączone.
DEBUG_GEOGRAPHY_EEA W przypadku urządzeń debugowania dane geograficzne są takie same jak w EOG.
DEBUG_GEOGRAPHY_NOT_EEA Obszar geograficzny jest widoczny poza EOG w przypadku urządzeń do debugowania.

Pamiętaj, że ustawienia debugowania działają tylko na urządzeniach testowych. Nie trzeba dodawać emulatorów do listy identyfikatorów urządzeń, ponieważ testy są już domyślnie włączone.

Testowanie aplikacji za pomocą pakietu UMP SDK może pomóc w zresetowaniu stanu pakietu SDK, aby można było symulować pierwsze instalacje użytkownika. SDK udostępnia w tym celu reset() metodę.

Java

consentInformation.reset();

Kotlin

consentInformation.reset()

Musisz też wywołać reset() , jeśli postanowisz całkowicie usunąć pakiet UMP SDK z projektu.