เริ่มใช้งาน

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

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

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

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

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

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

ติดตั้ง SDK

  1. ทําตามขั้นตอนเพื่อติดตั้ง Firebase C++ SDK UMP C++ SDK รวมอยู่ใน Firebase C++ SDK

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

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

    • ใน Android คุณต้องส่ง JNIEnv และ Activity ที่ NDK ระบุ คุณต้องทำขั้นตอนนี้เพียงครั้งแรกที่โทรหา GetInstance()
    • หรือหากใช้ Firebase C++ SDK ในแอปอยู่แล้ว คุณก็ส่ง firebase::App ได้ในครั้งแรกที่เรียกใช้ GetInstance()
    #include "firebase/ump/ump.h"
    
    namespace ump = ::firebase::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 เพื่อตรวจสอบการดำเนินการแบบอะซิงโครนัส

A firebase::Future ช่วยให้คุณทราบสถานะการเรียกเมธอดแบบไม่พร้อมกัน

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

คุณรับผลลัพธ์จาก Future ได้ 2 วิธี ดังนี้

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

หลังจากดำเนินการแบบไม่พร้อมกันเสร็จสมบูรณ์แล้ว คุณควรตรวจสอบ Futureของ error() เพื่อรับรหัสข้อผิดพลาดของการดำเนินการ หากรหัสข้อผิดพลาดคือ 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 ได้ที่เอกสารประกอบ Firebase C++ SDK และเอกสารประกอบ GMA C++ SDK

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

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

namespace ump = ::firebase::ump;

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());
      }
      // Consent information is successfully updated.
    });
}

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

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

#include "firebase/ump/ump.h"

namespace ump = ::firebase::ump;

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()

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

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

ขอโฆษณาโดยได้รับความยินยอมจากผู้ใช้

ก่อนขอโฆษณา ให้ใช้ ConsentInfo::GetInstance()‑> CanRequestAds() เพื่อตรวจสอบว่าคุณได้รับความยินยอมจากผู้ใช้หรือไม่

คุณสามารถตรวจสอบว่าขอโฆษณาขณะรวบรวมความยินยอมได้หรือไม่ในที่ต่อไปนี้

  • หลังจาก UMP SDK รวบรวมความยินยอมในเซสชันปัจจุบัน
  • ทันทีหลังจากที่คุณโทรหา RequestConsentInfoUpdate() UMP SDK อาจ ได้รับความยินยอมในเซสชันแอปก่อนหน้า

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

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

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

namespace gma = ::firebase::gma;
namespace ump = ::firebase::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 หรือสหราชอาณาจักร โดยใช้ 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();