Google User Messaging Platform (UMP) SDK 是一种隐私权和消息工具,用于 可帮助您管理隐私选项。如需了解详情,请参阅“隐私权和消息”页面简介。
前提条件
- Android API 级别 21 或更高级别 (适用于 Android)
创建消息类型
使用以下任一方式创建用户消息: 可用的用户消息类型 隐私权和消息功能标签页 AdMob 。UMP SDK 会尝试显示 使用 AdMob 应用 ID 创建的隐私权消息 。
有关详情,请参阅 隐私权和消息简介。
安装 SDK
按照相关步骤安装 Google 移动广告 (GMA) C++ SDK。UMP C++ SDK 包含在 GMA C++ SDK 中。
请确保配置您的应用的 AdMob 应用 ID 然后再继续。
在代码中,通过调用
ConsentInfo::GetInstance()
来初始化 UMP SDK。- 在 Android 上,您需要传入 NDK 提供的
JNIEnv
和Activity
。您只需在首次调用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
- 在 Android 上,您需要传入 NDK 提供的
对 ConsentInfo::GetInstance()
的后续调用均返回相同的实例。
如果您已完成 UMP SDK 的使用,可以通过删除 ConsentInfo
实例来关闭该 SDK:
void ShutdownUserMessagingPlatform() {
ump::ConsentInfo* consent_info = ump::ConsentInfo::GetInstance();
delete consent_info;
}
使用 Future
监控异步操作
firebase::Future
提供了一种方法来确定异步方法调用的完成状态。
异步运行的所有 UMP C++ 函数和方法调用都会返回
Future
,并提供“last result”(上次结果)函数来检索 Future
最近的操作
您可以通过以下两种方式从 Future
获取结果:
- 调用
OnCompletion()
, 您可以传入自己的回调函数,当操作发生时,系统会调用该函数 。 - 定期检查
Future
的status()
。当 状态 从kFutureStatusPending
更改为kFutureStatusCompleted
, 操作已完成。
异步操作完成后,您应检查 Future
的 error()
以获取操作的错误代码。如果错误代码为 0
(kConsentRequestSuccess
或 kConsentFormSuccess
),则表示操作已成功完成;否则,请检查错误代码和 error_message()
,以确定出错原因。
完成回调
以下示例展示了如何使用 OnCompletion
设置完成回调。
系统会在异步操作完成时调用该方法。
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.
}
});
}
更新循环轮询
在此示例中,在应用启动时启动异步操作后, 系统会在游戏更新循环函数(运行 每帧一次)。
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 文档。
添加应用 ID
您可以在 AdMob 界面。 将此 ID 添加到您的 替换为以下代码段:
征求用户同意
您应该在每个应用中请求更新用户的用户意见征求信息
使用 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.
}
});
}
如需查看使用命令检查完成情况的示例,请参阅上文 更新循环轮询,而不是完成回调。
根据需要加载并显示隐私权消息表单
获得最新的意见征求状态后,请调用
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.
}
});
}
});
}
如果您需要在用户做出选择或关闭表单后执行操作,请将相应逻辑放入处理 LoadAndShowConsentFormIfRequired()
返回的 Future
的代码中。
隐私设置选项
某些隐私权消息表单会通过发布商呈现的隐私权选项入口点显示,以便用户随时管理其隐私权选项。如需详细了解用户会看到哪些消息,请前往隐私权选项 入口点,请参阅 可用的用户消息类型。
如需实现隐私权选项入口点,请完成以下步骤:
- 请选中
getPrivacyOptionsRequirementStatus()
。 - 如果隐私选项入口点 您需要向应用添加一个可见且可互动的界面元素。
- 使用以下方式触发隐私权选项表单:
showPrivacyOptionsForm()
。
以下代码示例演示了这些步骤:
提出广告请求
在您的应用中发出广告请求之前,请检查您是否已征得用户同意
使用 ConsentInfo::GetInstance()‑>CanRequestAds()
。在征求用户意见时,您可以通过以下两种方式进行检查:
- 在当前会话中征求用户意见后。
- 致电
RequestConsentInfoUpdate()
后立即。 可能已在上一次会话中征得用户同意。延迟时间最佳实践:我们建议您不要等待回调完成,以便在应用启动后尽快开始加载广告。
如果在征求用户意见的过程中出现错误,您仍应 来请求广告。UMP SDK 会使用之前的 会话。
下面的完整示例使用了更新循环轮询,但您也可以使用
OnCompletion
回调来监控异步操作。使用
哪种方法更适合您的代码结构。
#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;
}
}
}
}
测试
如果您希望在应用开发过程中测试集成,请按照以下步骤以编程方式注册您的测试设备。请务必移除 代码,在您发布应用之前设置这些测试设备 ID。
- 调用
RequestConsentInfoUpdate()
。 检查日志输出,以查找类似于以下示例的消息(向您显示您的设备 ID 以及如何将设备添加为测试设备):
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]
将测试设备 ID 复制到剪贴板。
将代码修改为
ConsentRequestParameters.debug_settings.debug_device_ids
您的测试设备 ID 列表。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 提供了 Reset()
方法。
ConsentInfo::GetInstance()->Reset();