เริ่มต้น

ภายใต้ความยินยอมของผู้ใช้ EU ของ Google คุณต้อง เปิดเผยข้อมูลบางอย่างแก่ผู้ใช้ในเขตเศรษฐกิจยุโรป (EEA) กับสหราชอาณาจักร และได้รับความยินยอมในการใช้คุกกี้หรือพื้นที่เก็บข้อมูลอื่นๆ ในเครื่อง ในกรณีที่กฎหมายกำหนด และใช้ข้อมูลส่วนตัว (เช่น AdID) ในการแสดงโฆษณา นโยบายนี้เป็นผลมาจากข้อกำหนดด้าน ePrivacy ของสหภาพยุโรปและ กฎระเบียบให้ความคุ้มครองข้อมูลส่วนบุคคลของผู้บริโภค (GDPR)

เพื่อสนับสนุนผู้เผยแพร่โฆษณาให้ปฏิบัติตามหน้าที่ของตนภายใต้นโยบายนี้ Google ได้นำเสนอ SDK สำหรับ User Messaging Platform (UMP) UMP SDK ได้รับการอัปเดตให้รองรับ มาตรฐานล่าสุดของ IAB การกำหนดค่าทั้งหมดนี้สามารถ จัดการใน Ad Manager ความเป็นส่วนตัวและ การรับส่งข้อความ

ข้อกำหนดเบื้องต้น

  • API ของ Android ระดับ 21 ขึ้นไป

สร้างประเภทข้อความ

สร้างข้อความสำหรับผู้ใช้ด้วย ประเภทข้อความสำหรับผู้ใช้ที่ใช้ได้ ภายใต้ความเป็นส่วนตัวและ แท็บการรับส่งข้อความ Ad Manager ของคุณได้ UMP SDK พยายามแสดง ข้อความสำหรับผู้ใช้ที่สร้างจาก Ad Manager รหัสแอปพลิเคชัน ที่ตั้งไว้ในโปรเจ็กต์ หากไม่มีการกำหนดค่าข้อความสำหรับแอปพลิเคชันของคุณ SDK แสดงผลข้อผิดพลาด

ดูรายละเอียดเพิ่มเติมได้ที่ เกี่ยวกับความเป็นส่วนตัวและการรับส่งข้อความ

ติดตั้งด้วย Gradle

เพิ่มทรัพยากร Dependency สำหรับ SDK สำหรับ User Messaging Platform ของ Google ลงใน ไฟล์ Gradle ระดับแอป ปกติอยู่ที่ app/build.gradle

dependencies {
  implementation("com.google.android.ump:user-messaging-platform:2.2.0")
}

หลังจากทำการเปลี่ยนแปลงในbuild.gradleของแอปแล้ว โปรดตรวจสอบว่าได้ซิงค์ โดยใช้ไฟล์ Gradle

เพิ่มรหัสแอปพลิเคชัน

คุณดูรหัสแอปพลิเคชันได้ใน UI ของ Ad Manager เพิ่มบัตรประจำตัวลงใน AndroidManifest.xml ด้วยข้อมูลโค้ดต่อไปนี้

<manifest>
  <application>
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
  </application>
</manifest>

คุณควรขออัปเดตข้อมูลความยินยอมของผู้ใช้ในทุกแอป เปิดโดยใช้ requestConsentInfoUpdate()สิ่งนี้จะกำหนด ผู้ใช้ต้องให้ความยินยอมในกรณีที่ยังไม่ได้ดำเนินการ หรือ หากความยินยอมหมดอายุ

ต่อไปนี้เป็นตัวอย่างวิธีตรวจสอบสถานะจาก MainActivity ใน onCreate() วิธี

Java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

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;

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

    // Create a ConsentRequestParameters object.
    ConsentRequestParameters params = new ConsentRequestParameters
        .Builder()
        .build();

    consentInformation = UserMessagingPlatform.getConsentInformation(this);
    consentInformation.requestConsentInfoUpdate(
        this,
        params,
        (OnConsentInfoUpdateSuccessListener) () -> {
          // TODO: Load and show the consent form.
        },
        (OnConsentInfoUpdateFailureListener) requestConsentError -> {
          // Consent gathering failed.
          Log.w(TAG, String.format("%s: %s",
              requestConsentError.getErrorCode(),
              requestConsentError.getMessage()));
        });
  }
}

Kotlin

package com.example.myapplication

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

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

    // Create a ConsentRequestParameters object.
    val params = ConsentRequestParameters
        .Builder()
        .build()

    consentInformation = UserMessagingPlatform.getConsentInformation(this)
    consentInformation.requestConsentInfoUpdate(
        this,
        params,
        ConsentInformation.OnConsentInfoUpdateSuccessListener {
          // TODO: Load and show the consent form.
        },
        ConsentInformation.OnConsentInfoUpdateFailureListener {
          requestConsentError ->
          // Consent gathering failed.
          Log.w(TAG, "${requestConsentError.errorCode}: ${requestConsentError.message}")
        })
  }
}

โหลดและแสดงแบบฟอร์มความยินยอมหากจำเป็น

หลังจากได้รับสถานะความยินยอมล่าสุดแล้ว โปรดโทร loadAndShowConsentFormIfRequired() ใน ConsentForm เพื่อโหลดแบบฟอร์มความยินยอม หาก ต้องระบุสถานะความยินยอม SDK จะโหลดแบบฟอร์มและแสดงทันที จาก activityที่ระบุ callback ระบบจะเรียก หลังจากส่งแบบฟอร์มแล้ว หากไม่จำเป็นต้องได้รับความยินยอม callback ระบบจะเรียกใช้ ทันที

Java

public class MainActivity extends AppCompatActivity {
  private ConsentInformation consentInformation;

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

    // Create a ConsentRequestParameters object.
    ConsentRequestParameters params = new ConsentRequestParameters
        .Builder()
        .build();

    consentInformation = UserMessagingPlatform.getConsentInformation(this);
    consentInformation.requestConsentInfoUpdate(
        this,
        params,
        (OnConsentInfoUpdateSuccessListener) () -> {
          UserMessagingPlatform.loadAndShowConsentFormIfRequired(
            this,
            (OnConsentFormDismissedListener) loadAndShowError -> {
              if (loadAndShowError != null) {
                // Consent gathering failed.
                Log.w(TAG, String.format("%s: %s",
                    loadAndShowError.getErrorCode(),
                    loadAndShowError.getMessage()));
              }

              // Consent has been gathered.
            }
          );
        },
        (OnConsentInfoUpdateFailureListener) requestConsentError -> {
          // Consent gathering failed.
          Log.w(TAG, String.format("%s: %s",
              requestConsentError.getErrorCode(),
              requestConsentError.getMessage()));
        });
  }
}

Kotlin

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

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

    // Create a ConsentRequestParameters object.
    val params = ConsentRequestParameters
        .Builder()
        .build()

    consentInformation = UserMessagingPlatform.getConsentInformation(this)
    consentInformation.requestConsentInfoUpdate(
        this,
        params,
        ConsentInformation.OnConsentInfoUpdateSuccessListener {
          UserMessagingPlatform.loadAndShowConsentFormIfRequired(
            this@MainActivity,
            ConsentForm.OnConsentFormDismissedListener {
              loadAndShowError ->
              if (loadAndShowError != null) {
                // Consent gathering failed.
                Log.w(TAG, "${loadAndShowError.errorCode}: ${loadAndShowError.message}")
              }

              // Consent has been gathered.
            }
          )
        },
        ConsentInformation.OnConsentInfoUpdateFailureListener {
          requestConsentError ->
          // Consent gathering failed.
          Log.w(TAG, "${requestConsentError.errorCode}: ${requestConsentError.message}")
        })
  }
}

หากคุณจำเป็นต้องดำเนินการใดๆ หลังจากที่ผู้ใช้เลือกหรือยกเลิก วางตรรกะนั้นในตัวแปร callback สำหรับแบบฟอร์มของคุณ

ส่งคำขอแสดงโฆษณา

ก่อนขอโฆษณาในแอป ให้ตรวจสอบว่าคุณได้รับความยินยอมแล้วหรือไม่ จากผู้ใช้ที่ใช้ canRequestAds()มี 2 แบบ สิ่งที่ต้องตรวจสอบขณะรวบรวมความยินยอม:

  1. เมื่อรวบรวมความยินยอมในเซสชันปัจจุบันแล้ว
  2. ทันทีที่คุณโทรหา requestConsentInfoUpdate() เป็นไปได้ว่าอาจได้รับความยินยอมแล้วในเซสชันก่อนหน้านี้ แบบเวลาในการตอบสนอง แนวทางปฏิบัติแนะนำคืออย่ารอให้การติดต่อกลับเสร็จสิ้นเพื่อให้คุณสามารถ ให้เริ่มโหลดโฆษณาโดยเร็วที่สุดเท่าที่จะเป็นไปได้หลังจากที่คุณเปิดตัวแอป

หากเกิดข้อผิดพลาดขึ้นในระหว่างกระบวนการรวบรวมความยินยอม คุณควร ในการขอโฆษณา UMP SDK ใช้สถานะความยินยอมจาก เซสชัน

Java

public class MainActivity extends AppCompatActivity {
  private ConsentInformation consentInformation;
  // Use an atomic boolean to initialize the Google Mobile Ads SDK and load ads once.
  private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create a ConsentRequestParameters object.
    ConsentRequestParameters params = new ConsentRequestParameters
        .Builder()
        .build();

    consentInformation = UserMessagingPlatform.getConsentInformation(this);
    consentInformation.requestConsentInfoUpdate(
        this,
        params,
        (OnConsentInfoUpdateSuccessListener) () -> {
          UserMessagingPlatform.loadAndShowConsentFormIfRequired(
            this,
            (OnConsentFormDismissedListener) loadAndShowError -> {
              if (loadAndShowError != null) {
                // Consent gathering failed.
                Log.w(TAG, String.format("%s: %s",
                    loadAndShowError.getErrorCode(),
                    loadAndShowError.getMessage()));
              }

              // Consent has been gathered.
              if (consentInformation.canRequestAds()) {
                initializeMobileAdsSdk();
              }
            }
          );
        },
        (OnConsentInfoUpdateFailureListener) requestConsentError -> {
          // Consent gathering failed.
          Log.w(TAG, String.format("%s: %s",
              requestConsentError.getErrorCode(),
              requestConsentError.getMessage()));
        });
    
    // Check if you can initialize the Google Mobile Ads SDK in parallel
    // while checking for new consent information. Consent obtained in
    // the previous session can be used to request ads.
    if (consentInformation.canRequestAds()) {
      initializeMobileAdsSdk();
    }
  }
  
  private void initializeMobileAdsSdk() {
    if (isMobileAdsInitializeCalled.getAndSet(true)) {
      return;
    }

    new Thread(
            () -> {
              // Initialize the Google Mobile Ads SDK on a background thread.
              
              MobileAds.initialize(this, initializationStatus -> {});
              runOnUiThread(
                  () -> {
                    // TODO: Request an ad.
                    // loadInterstitialAd();
                  });
              
            })
        .start();
  }
}

Kotlin

class MainActivity : AppCompatActivity() {
  private lateinit var consentInformation: ConsentInformation
  // Use an atomic boolean to initialize the Google Mobile Ads SDK and load ads once.
  private var isMobileAdsInitializeCalled = AtomicBoolean(false)
  
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // Create a ConsentRequestParameters object.
    val params = ConsentRequestParameters
        .Builder()
        .build()

    consentInformation = UserMessagingPlatform.getConsentInformation(this)
    consentInformation.requestConsentInfoUpdate(
        this,
        params,
        ConsentInformation.OnConsentInfoUpdateSuccessListener {
          UserMessagingPlatform.loadAndShowConsentFormIfRequired(
            this@MainActivity,
            ConsentForm.OnConsentFormDismissedListener {
              loadAndShowError ->
              if (loadAndShowError != null) {
                // Consent gathering failed.
                Log.w(TAG, "${loadAndShowError.errorCode}: ${loadAndShowError.message}")
              }

              // Consent has been gathered.
              if (consentInformation.canRequestAds()) {
                initializeMobileAdsSdk()
              }
            }
          )
        },
        ConsentInformation.OnConsentInfoUpdateFailureListener {
          requestConsentError ->
          // Consent gathering failed.
          Log.w(TAG, "${requestConsentError.errorCode}: ${requestConsentError.message}")
        })
    
    // Check if you can initialize the Google Mobile Ads SDK in parallel
    // while checking for new consent information. Consent obtained in
    // the previous session can be used to request ads.
    if (consentInformation.canRequestAds()) {
      initializeMobileAdsSdk()
    }
  }
  
  private fun initializeMobileAdsSdk() {
    if (isMobileAdsInitializeCalled.getAndSet(true)) {
      return
    }

    val backgroundScope = CoroutineScope(Dispatchers.IO)
    backgroundScope.launch {
      
      MobileAds.initialize(this@MainActivity) {}
      runOnUiThread {
        // TODO: Request an ad.
        // loadInterstitialAd()
      }
      
    }
  }
}

ตัวเลือกความเป็นส่วนตัว

แบบฟอร์มความยินยอมบางแบบฟอร์มกำหนดให้ผู้ใช้แก้ไขความยินยอมได้ทุกเมื่อ ปฏิบัติตาม ตามขั้นตอนต่อไปนี้เพื่อใช้ปุ่มตัวเลือกความเป็นส่วนตัว หากจำเป็น

ทั้งสองสามารถทำได้โดยดำเนินการดังนี้

  1. ใช้องค์ประกอบ UI เช่น ปุ่มในหน้าการตั้งค่าของแอป ที่สามารถเรียกใช้แบบฟอร์มตัวเลือกความเป็นส่วนตัว
  2. เมื่อทำเสร็จแล้ว loadAndShowConsentFormIfRequired() ให้ตรวจสอบ privacyOptionsRequirementStatus() เพื่อพิจารณาว่าจะแสดง องค์ประกอบ UI ที่แสดงแบบฟอร์มตัวเลือกความเป็นส่วนตัวได้
  3. เมื่อผู้ใช้โต้ตอบกับองค์ประกอบ UI ของคุณ ให้เรียกใช้ showPrivacyOptionsForm() เพื่อแสดงแบบฟอร์มเพื่อให้ผู้ใช้ อัปเดตตัวเลือกความเป็นส่วนตัวได้ทุกเมื่อ

ตัวอย่างต่อไปนี้แสดงวิธีนำเสนอแบบฟอร์มตัวเลือกความเป็นส่วนตัวจาก MenuItem

Java

private final ConsentInformation consentInformation;

// Show a privacy options button if required.
public boolean isPrivacyOptionsRequired() {
  return consentInformation.getPrivacyOptionsRequirementStatus()
      == PrivacyOptionsRequirementStatus.REQUIRED;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
  // ...

  consentInformation = UserMessagingPlatform.getConsentInformation(this);
  consentInformation.requestConsentInfoUpdate(
      this,
      params,
      (OnConsentInfoUpdateSuccessListener) () -> {
        UserMessagingPlatform.loadAndShowConsentFormIfRequired(
          this,
          (OnConsentFormDismissedListener) loadAndShowError -> {
            // ...

            // Consent has been gathered.

            if (isPrivacyOptionsRequired()) {
              // Regenerate the options menu to include a privacy setting.
              invalidateOptionsMenu();
            }
          }
        )
      }
      // ...
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.action_menu, menu);
  MenuItem moreMenu = menu.findItem(R.id.action_more);
  moreMenu.setVisible(isPrivacyOptionsRequired());
  return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  // ...

  popup.setOnMenuItemClickListener(
      popupMenuItem -> {
        if (popupMenuItem.getItemId() == R.id.privacy_settings) {
          // Present the privacy options form when a user interacts with
          // the privacy settings button.
          UserMessagingPlatform.showPrivacyOptionsForm(
              this,
              formError -> {
                if (formError != null) {
                  // Handle the error.
                }
              }
          );
          return true;
        }
        return false;
      });
  return super.onOptionsItemSelected(item);
}

Kotlin

private val consentInformation: ConsentInformation =
  UserMessagingPlatform.getConsentInformation(context)

// Show a privacy options button if required.
val isPrivacyOptionsRequired: Boolean
  get() =
    consentInformation.privacyOptionsRequirementStatus ==
      ConsentInformation.PrivacyOptionsRequirementStatus.REQUIRED

override fun onCreate(savedInstanceState: Bundle?) {
  ...
  consentInformation = UserMessagingPlatform.getConsentInformation(this)
  consentInformation.requestConsentInfoUpdate(
      this,
      params,
      ConsentInformation.OnConsentInfoUpdateSuccessListener {
        UserMessagingPlatform.loadAndShowConsentFormIfRequired(
          this@MainActivity,
          ConsentForm.OnConsentFormDismissedListener {
            // ...

            // Consent has been gathered.

            if (isPrivacyOptionsRequired) {
              // Regenerate the options menu to include a privacy setting.
              invalidateOptionsMenu();
            }
          }
        )
      }
      // ...
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
  menuInflater.inflate(R.menu.action_menu, menu)
  menu?.findItem(R.id.action_more)?.apply {
    isVisible = isPrivacyOptionsRequired
  }
  return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
  // ...

  popup.setOnMenuItemClickListener { popupMenuItem ->
    when (popupMenuItem.itemId) {
      R.id.privacy_settings -> {
        // Present the privacy options form when a user interacts with
        // the privacy settings button.
        UserMessagingPlatform.showPrivacyOptionsForm(this) { formError ->
          formError?.let {
            // Handle the error.
          }
        }
        true
      }
      else -> false
    }
  }
  return super.onOptionsItemSelected(item)
}

การทดสอบ

หากต้องการทดสอบการผสานรวมในแอประหว่างการพัฒนา ให้ทำตาม ขั้นตอนเหล่านี้เพื่อลงทะเบียนอุปกรณ์ทดสอบแบบเป็นโปรแกรม อย่าลืมนำ ที่ตั้งค่ารหัสอุปกรณ์ทดสอบเหล่านี้ก่อนที่คุณจะเผยแพร่แอป

  1. โทร requestConsentInfoUpdate()
  2. ตรวจสอบเอาต์พุตของบันทึกสำหรับข้อความที่คล้ายกับตัวอย่างต่อไปนี้ ซึ่ง แสดงรหัสอุปกรณ์และวิธีเพิ่มรหัสเป็นอุปกรณ์ทดสอบ

    Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("33BE2250B43518CCDA7DE426D04EE231") to set this as a debug device.
    
  3. คัดลอกรหัสอุปกรณ์ทดสอบไปยังคลิปบอร์ด

  4. แก้ไขโค้ด โทร DebugGeography.TestDeviceHashedIds และข้ามเข้า รายการรหัสอุปกรณ์ทดสอบ

บังคับระบุภูมิศาสตร์

UMP SDK ให้คุณทดสอบลักษณะการทำงานของแอปได้เสมือนว่าอุปกรณ์ ที่อยู่ใน EEA หรือสหราชอาณาจักรโดยใช้ the setDebugGeography method which takes a DebugGeography on ConsentDebugSettings.Builderโปรดทราบว่า การตั้งค่าการแก้ไขข้อบกพร่องใช้ได้เฉพาะกับอุปกรณ์ทดสอบ

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 | Kotlin