เริ่มใช้งาน

SDK สำหรับ User Messaging Platform (UMP) ของ Google เป็นเครื่องมือด้านความเป็นส่วนตัวและการแสดงข้อความแจ้งผู้ใช้เพื่อ ช่วยคุณจัดการตัวเลือกความเป็นส่วนตัว ดูข้อมูลเพิ่มเติมได้ที่ เกี่ยวกับความเป็นส่วนตัวและการแสดงข้อความแจ้งผู้ใช้ คุณดูการติดตั้งใช้งาน IMA ที่ใช้งานได้กับ UMP SDK ได้ในแอปตัวอย่าง UMP

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

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

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

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

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

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

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

dependencies {
  implementation("com.google.android.ump:user-messaging-platform:3.1.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>

หากต้องการรับข้อมูลความยินยอมของผู้ใช้ ให้ทำดังนี้

ประกาศอินสแตนซ์ของ 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 is complete.
}

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

แบบฟอร์มข้อความด้านความเป็นส่วนตัวบางแบบจะแสดงจากจุดแรกเข้าของตัวเลือกความเป็นส่วนตัวที่ผู้เผยแพร่โฆษณาแสดงผล ซึ่งช่วยให้ผู้ใช้จัดการตัวเลือกความเป็นส่วนตัวได้ทุกเมื่อ ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อความที่ผู้ใช้เห็นที่จุดแรกเข้าของตัวเลือกความเป็นส่วนตัวได้ที่ ประเภทข้อความผู้ใช้ที่ใช้ได้

ตรวจสอบว่าต้องมีจุดแรกเข้าของตัวเลือกความเป็นส่วนตัวหรือไม่

หลังจากเรียกใช้ requestConsentInfoUpdate() แล้ว ให้ตรวจสอบ getPrivacyOptionsRequirementStatus() เพื่อ ดูว่าแอปของคุณต้องมีจุดแรกเข้าของตัวเลือกความเป็นส่วนตัวหรือไม่ หากต้องมีจุดแรกเข้า ให้เพิ่มองค์ประกอบ UI ที่มองเห็นได้และโต้ตอบได้ลงในแอปซึ่งแสดง แบบฟอร์มตัวเลือกความเป็นส่วนตัว หากไม่จำเป็นต้องมีจุดแรกเข้าด้านความเป็นส่วนตัว ให้กำหนดค่าองค์ประกอบ UI ให้ไม่แสดงและโต้ตอบไม่ได้

Java


/** Helper function to determine if a privacy options entry point 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 ใช้สถานะความยินยอมจากเซสชันแอปก่อนหน้า

ป้องกันการทำงานของคำขอโฆษณาที่ซ้ำซ้อน

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

การทดสอบ

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

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

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

  4. แก้ไขโค้ดเพื่อเรียกใช้ ConsentDebugSettings.Builder().addTestDeviceHashedId()และส่ง รายการรหัสอุปกรณ์ทดสอบ

    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,
        // ...
    )
    

บังคับใช้ภูมิศาสตร์

SDK ของ UMP มีวิธีทดสอบลักษณะการทำงานของแอปเสมือนว่าอุปกรณ์อยู่ในภูมิภาคต่างๆ เช่น EEA หรือสหราชอาณาจักร โดยใช้ DebugGeography โปรดทราบว่าการตั้งค่าการแก้ไขข้อบกพร่องจะใช้ได้เฉพาะในอุปกรณ์ทดสอบเท่านั้น

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 แบบเต็มที่อธิบายไว้ในหน้านี้ได้ที่ UmpExample