เริ่มเลย

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

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

  • API ของ Android ระดับ 21 ขึ้นไป (สำหรับ Android)

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

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

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

ติดตั้ง SDK

  1. ทำตามขั้นตอนเพื่อติดตั้ง Google Mobile Ads (GMA) C++ SDK UMP C++ SDK รวมอยู่ใน GMA C++ SDK

  2. ตรวจสอบว่าคุณกำหนดค่าแอป AdMob ของแอปแล้ว รหัสในโปรเจ็กต์ ก่อนดำเนินการต่อ

  3. ในโค้ด ให้เริ่มต้น UMP SDK โดยการเรียกใช้ ConsentInfo::GetInstance()

    • บน Android คุณจะต้องส่ง JNIEnv และ Activity ซึ่งจัดเตรียมไว้โดย NDK คุณต้องดำเนินการนี้ในครั้งแรกที่โทรหา GetInstance() เท่านั้น
    • หรือหากคุณใช้ Firebase C++ อยู่แล้ว SDK ในแอปของคุณ คุณสามารถส่ง ใน firebase::App ครั้งแรกที่คุณโทรหา GetInstance()
    #include "firebase/gma/ump.h"
    
    namespace ump = ::firebase::gma::ump;
    
    // Initialize using a firebase::App
    void InitializeUserMessagingPlatform(const firebase::App& app) {
      ump::ConsentInfo* consent_info = ump::ConsentInfo::GetInstance(app);
    }
    
    // Initialize without a firebase::App
    #ifdef ANDROID
    void InitializeUserMessagingPlatform(JNIEnv* jni_env, jobject activity) {
      ump::ConsentInfo* consent_info = ump::ConsentInfo::GetInstance(jni_env, activity);
    }
    #else  // non-Android
    void InitializeUserMessagingPlatform() {
      ump::ConsentInfo* consent_info = ump::ConsentInfo::GetInstance();
    }
    #endif
    

การเรียก ConsentInfo::GetInstance() ครั้งต่อๆ ไปทั้งหมดจะแสดงผลอินสแตนซ์เดียวกัน

หากใช้ UMP SDK เสร็จแล้ว คุณสามารถปิด SDK ดังกล่าวได้โดยลบ อินสแตนซ์ ConsentInfo รายการ:

void ShutdownUserMessagingPlatform() {
  ump::ConsentInfo* consent_info = ump::ConsentInfo::GetInstance();
  delete consent_info;
}

ใช้ Future เพื่อตรวจสอบการดำเนินการที่ไม่พร้อมกัน

firebase::Future แสดงวิธีการพิจารณาสถานะเสร็จสมบูรณ์ของเมธอดอะซิงโครนัส

ฟังก์ชัน UMP C++ และการเรียกเมธอดทั้งหมดที่ดำเนินการแบบไม่พร้อมกันจะแสดงผล Future และระบุ "ผลลัพธ์ล่าสุด" ด้วย เพื่อเรียกข้อมูล Future จากการดำเนินการล่าสุด

มี 2 วิธีในการรับผลลัพธ์จาก Future ได้แก่

  1. โทร OnCompletion() ในฟังก์ชัน Callback ของคุณเอง ซึ่งจะเรียกใช้เมื่อการดำเนินการ เสร็จสมบูรณ์
  2. ตรวจสอบ status() ของ Future เป็นระยะ เมื่อ สถานะ เปลี่ยนจาก kFutureStatusPending เป็น kFutureStatusCompleted การดำเนินการเสร็จสมบูรณ์แล้ว

หลังจากการทำงานแบบอะซิงโครนัสเสร็จสิ้น คุณควรตรวจสอบ error() ของ Future เพื่อรับข้อผิดพลาดของการดำเนินการ โค้ด หากรหัสข้อผิดพลาดคือ 0 (kConsentRequestSuccess หรือ kConsentFormSuccess) การดำเนินการเสร็จสมบูรณ์ ไม่เช่นนั้น ให้ตรวจสอบรหัสข้อผิดพลาดและ error_message() เพื่อตรวจสอบว่าเกิดข้อผิดพลาดใด

การติดต่อกลับเพื่อเสร็จสิ้น

ต่อไปนี้คือตัวอย่างวิธีใช้ OnCompletion เพื่อตั้งค่า Callback ที่เสร็จสมบูรณ์ ซึ่งจะถูกเรียกเมื่อการทำงานแบบอะซิงโครนัสเสร็จสิ้น

void MyApplicationStart() {
  // [... other app initialization code ...]

  ump::ConsentInfo *consent_info = ump::ConsentInfo::GetInstance();

  // See the section below for more information about RequestConsentInfoUpdate.
  firebase::Future<void> result = consent_info->RequestConsentInfoUpdate(...);

  result.OnCompletion([](const firebase::Future<void>& req_result) {
    if (req_result.error() == ump::kConsentRequestSuccess) {
      // Operation succeeded. You can now call LoadAndShowConsentFormIfRequired().
    } else {
      // Operation failed. Check req_result.error_message() for more information.
    }
  });
}

อัปเดตการสำรวจแบบวนซ้ำ

ในตัวอย่างนี้ หลังจากที่การดำเนินการแบบไม่พร้อมกันเริ่มต้นเมื่อเปิดแอป ที่อื่น ในฟังก์ชันการวนการอัปเดตของเกม (ซึ่งเรียกใช้ 1 ครั้งต่อเฟรม)

ump::ConsentInfo *g_consent_info = nullptr;
bool g_waiting_for_request = false;

void MyApplicationStart() {
  // [... other app initialization code ...]

  g_consent_info = ump::ConsentInfo::GetInstance();
  // See the section below for more information about RequestConsentInfoUpdate.
  g_consent_info->RequestConsentInfoUpdate(...);
  g_waiting_for_request = true;
}

// Elsewhere, in the game's update loop, which runs once per frame:
void MyGameUpdateLoop() {
  // [... other game logic here ...]

  if (g_waiting_for_request) {
    // Check whether RequestConsentInfoUpdate() has finished.
    // Calling "LastResult" returns the Future for the most recent operation.
    firebase::Future<void> result =
      g_consent_info->RequestConsentInfoUpdateLastResult();

    if (result.status() == firebase::kFutureStatusComplete) {
      g_waiting_for_request = false;
      if (result.error() == ump::kConsentRequestSuccess) {
        // Operation succeeded. You can call LoadAndShowConsentFormIfRequired().
      } else {
        // Operation failed. Check result.error_message() for more information.
      }
    }
  }
}

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ firebase::Future โปรดดูที่ SDK ของ Firebase C++ เอกสารประกอบ และเอกสารประกอบเกี่ยวกับ SDK ของ GMA C++

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

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

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

  • กำหนดว่าต้องได้รับความยินยอมหรือไม่ เช่น ต้องได้รับความยินยอมสำหรับ เป็นครั้งแรก หรือคำตัดสินเกี่ยวกับความยินยอมก่อนหน้านี้หมดอายุ
  • ระบุว่าจำเป็นต้องมีจุดแรกเข้าของตัวเลือกความเป็นส่วนตัวหรือไม่ ประกาศเกี่ยวกับความเป็นส่วนตัวบางส่วน กำหนดให้แอปอนุญาตให้ผู้ใช้แก้ไขตัวเลือกความเป็นส่วนตัวได้ทุกเมื่อ
#include "firebase/gma/ump.h"

namespace ump = ::firebase::gma::ump;

void MyApplicationStart() {
  ump::ConsentInfo* consent_info = ump::ConsentInfo::GetInstance();

  // Create a ConsentRequestParameters struct.
  ump::ConsentRequestParameters params;
  // Set tag for under age of consent. False means users are NOT under age
  // of consent.
  params.tag_for_under_age_of_consent = false;

  consent_info->RequestConsentInfoUpdate(params).OnCompletion(
    [](const Future<void>& result) {
      if (result.error() != ump::kConsentRequestSuccess) {
        LogMessage("Error requesting consent update: %s", result.error_message());
      } else {
        // Consent status is now available.
      }
    });
}

โปรดดูตัวอย่างการตรวจสอบการทำงานเสร็จสมบูรณ์โดยใช้ด้านบน อัปเดตการสำรวจแบบวนซ้ำ แทนที่จะเป็น Callback ที่เสร็จสมบูรณ์

โหลดและนำเสนอแบบฟอร์มประกาศเกี่ยวกับความเป็นส่วนตัวหากจำเป็น

หลังจากได้รับสถานะความยินยอมล่าสุดแล้ว โปรดโทร LoadAndShowConsentFormIfRequired() เพื่อโหลดแบบฟอร์มใดๆ ที่ต้องใช้เพื่อ รวบรวมความยินยอมของผู้ใช้ หลังจากโหลดแล้ว แบบฟอร์มจะปรากฏขึ้นทันที

void MyApplicationStart(ump::FormParent parent) {
  ump::ConsentInfo* consent_info = ump::ConsentInfo::GetInstance();

  // Create a ConsentRequestParameters struct..
  ump::ConsentRequestParameters params;
  // Set tag for under age of consent. False means users are NOT under age of consent.
  params.tag_for_under_age_of_consent = false;

  consent_info->RequestConsentInfoUpdate(params).OnCompletion(
    [*](const Future<void>& req_result) {
      if (req_result.error() != ump::kConsentRequestSuccess) {
        // req_result.error() is a kConsentRequestError enum.
        LogMessage("Error requesting consent update: %s", req_result.error_message());
      } else {
        consent_info->LoadAndShowConsentFormIfRequired(parent).OnCompletion(
        [*](const Future<void>& form_result) {
          if (form_result.error() != ump::kConsentFormSuccess) {
            // form_result.error() is a kConsentFormError enum.
            LogMessage("Error showing privacy message form: %s", form_result.error_message());
          } else {
            // Either the form was shown and completed by the user, or consent was not required.
          }
        });
      }
    });
}
วันที่

หากคุณจำเป็นต้องดำเนินการใดๆ หลังจากที่ผู้ใช้เลือกหรือยกเลิก วางตรรกะดังกล่าวลงในโค้ดที่จัดการ Future LoadAndShowConsentFormIfRequired() ส่งคืน

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

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

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

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

ตัวอย่างโค้ดต่อไปนี้แสดงขั้นตอนเหล่านี้

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

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

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

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

ตัวอย่างที่สมบูรณ์ต่อไปนี้ใช้การสำรวจแบบวนซ้ำสำหรับอัปเดต แต่คุณสามารถใช้ OnCompletion Callback เพื่อตรวจสอบการดำเนินการแบบไม่พร้อมกัน ใช้ ไม่ว่าเทคนิคใดจะเหมาะกับโครงสร้างโค้ดของคุณมากกว่า

#include "firebase/future.h"
#include "firebase/gma/gma.h"
#include "firebase/gma/ump.h"

namespace gma = ::firebase::gma;
namespace ump = ::firebase::gma::ump;
using firebase::Future;

ump::ConsentInfo* g_consent_info = nullptr;
// State variable for tracking the UMP consent flow.
enum { kStart, kRequest, kLoadAndShow, kInitGma, kFinished, kErrorState } g_state = kStart;
bool g_ads_allowed = false;

void MyApplicationStart() {
  g_consent_info = ump::ConsentInfo::GetInstance(...);

  // Create a ConsentRequestParameters struct..
  ump::ConsentRequestParameters params;
  // Set tag for under age of consent. False means users are NOT under age of consent.
  params.tag_for_under_age_of_consent = false;

  g_consent_info->RequestConsentInfoUpdate(params);
  // CanRequestAds() can return a cached value from a previous run immediately.
  g_ads_allowed = g_consent_info->CanRequestAds();
  g_state = kRequest;
}

// This function runs once per frame.
void MyGameUpdateLoop() {
  // [... other game logic here ...]

  if (g_state == kRequest) {
    Future<void> req_result = g_consent_info->RequestConsentInfoUpdateLastResult();

    if (req_result.status() == firebase::kFutureStatusComplete) {
      g_ads_allowed = g_consent_info->CanRequestAds();
      if (req_result.error() == ump::kConsentRequestSuccess) {
        // You must provide the FormParent (Android Activity or iOS UIViewController).
        ump::FormParent parent = GetMyFormParent();
        g_consent_info->LoadAndShowConsentFormIfRequired(parent);
        g_state = kLoadAndShow;
      } else {
        LogMessage("Error requesting consent status: %s", req_result.error_message());
        g_state = kErrorState;
      }
    }
  }
  if (g_state == kLoadAndShow) {
    Future<void> form_result = g_consent_info->LoadAndShowConsentFormIfRequiredLastResult();

    if (form_result.status() == firebase::kFutureStatusComplete) {
      g_ads_allowed = g_consent_info->CanRequestAds();
      if (form_result.error() == ump::kConsentRequestSuccess) {
        if (g_ads_allowed) {
          // Initialize GMA. This is another asynchronous operation.
          firebase::gma::Initialize();
          g_state = kInitGma;
        } else {
          g_state = kFinished;
        }
        // Optional: shut down the UMP SDK to save memory.
        delete g_consent_info;
        g_consent_info = nullptr;
      } else {
        LogMessage("Error displaying privacy message form: %s", form_result.error_message());
        g_state = kErrorState;
      }
    }
  }
  if (g_state == kInitGma && g_ads_allowed) {
    Future<gma::AdapterInitializationStatus> gma_future = gma::InitializeLastResult();

    if (gma_future.status() == firebase::kFutureStatusComplete) {
      if (gma_future.error() == gma::kAdErrorCodeNone) {
        g_state = kFinished;
        // TODO: Request an ad.
      } else {
        LogMessage("Error initializing GMA: %s", gma_future.error_message());
        g_state = kErrorState;
      }
    }
  }
}

การทดสอบ

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

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

    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. คัดลอกรหัสอุปกรณ์ทดสอบไปยังคลิปบอร์ด

  4. แก้ไขโค้ด เป็นการตั้งค่า ConsentRequestParameters.debug_settings.debug_device_ids เพื่อ รายการรหัสอุปกรณ์ทดสอบ

    void MyApplicationStart() {
      ump::ConsentInfo consent_info = ump::ConsentInfo::GetInstance(...);
    
      ump::ConsentRequestParameters params;
      params.tag_for_under_age_of_consent = false;
      params.debug_settings.debug_device_ids = {"TEST-DEVICE-HASHED-ID"};
    
      consent_info->RequestConsentInfoUpdate(params);
    }
    

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

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

void MyApplicationStart() {
  ump::ConsentInfo consent_info = ump::ConsentInfo::GetInstance(...);

  ump::ConsentRequestParameters params;
  params.tag_for_under_age_of_consent = false;
  params.debug_settings.debug_device_ids = {"TEST-DEVICE-HASHED-ID"};
  // Geography appears as EEA for debug devices.
  params.debug_settings.debug_geography = ump::kConsentDebugGeographyEEA

  consent_info->RequestConsentInfoUpdate(params);
}

ในการทดสอบแอปด้วย UMP SDK คุณอาจพบว่าการรีเซ็ตฟังก์ชัน ของ SDK เพื่อให้คุณสามารถจำลองประสบการณ์การติดตั้งครั้งแรกของผู้ใช้ได้ SDK มี Reset() วิธีในการดำเนินการนี้

วันที่
  ConsentInfo::GetInstance()->Reset();

ตัวอย่างใน GitHub