Google User Messaging Platform (UMP) SDK 是一款隐私权和消息工具,可帮助您管理隐私权选择。如需了解详情,请参阅 隐私权和消息简介。
前提条件
- Android API 级别 21 或更高级别(适用于 Android)
创建消息类型
在 AdMob 账号的隐私权和消息 标签页下,使用其中一种 可用的用户消息类型 创建用户消息。UMP SDK 会尝试显示根据项目中设置的 AdMob 应用 ID 创建的隐私权消息。
如需了解详情,请参阅 隐私权和消息简介。
安装 SDK
按照步骤安装 Firebase C++ SDK。UMP C++ SDK 包含在 Firebase C++ SDK 中。
在代码中,通过调用
ConsentInfo::GetInstance()初始化 UMP SDK。- 在 Android 上,您需要传入 NDK 提供的
JNIEnv和Activity。您只需在首次调用GetInstance()时执行此操作。 - 或者,如果您已在应用中使用 Firebase C++
SDK,则可以在首次调用
GetInstance()时传入firebase::App。
#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- 在 Android 上,您需要传入 NDK 提供的
后续对 ConsentInfo::GetInstance() 的调用都会返回同一实例。
如果您已完成 UMP SDK 的使用,可以通过删除 ConsentInfo 实例来关闭该 SDK:
void ShutdownUserMessagingPlatform() {
ump::ConsentInfo* consent_info = ump::ConsentInfo::GetInstance();
delete consent_info;
}
使用 Future 监控异步操作
A
firebase::Future
提供了一种方法来确定异步方法
调用的完成状态。
所有以异步方式运行的 UMP C++ 函数和方法调用都会返回 Future,并且还会提供“上次结果”函数,以便从最近的操作中检索 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 文档。
获取用户的意见征求信息
每次启动应用时,您都应使用
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.
}
});
}
});
}
如需查看使用 更新循环轮询(而不是完成回调函数)检查完成情况的示例,请参阅上文。
如果您需要在用户做出选择或关闭表单后执行操作,请将相应逻辑放入处理 LoadAndShowConsentFormIfRequired() 返回的 Future 的代码中。
隐私设置选项
某些隐私权消息表单会通过发布商呈现的隐私设置选项入口点显示,让用户可以随时管理其隐私设置选项。 如需详细了解您的用户在隐私设置选项 入口点会看到哪一种消息,请参阅 可用的用户消息类型。
在征得用户同意后请求广告
在请求广告之前,请使用
ConsentInfo::GetInstance()‑>
CanRequestAds() 检查您是否已
征得用户同意:
以下列出了在收集用户意见时检查是否可以请求广告的位置:
- UMP SDK 在当前会话中收集用户意见后。
- 在您调用
RequestConsentInfoUpdate()后立即执行。UMP SDK 可能已在之前的应用会话中征得用户同意。
ConsentInfo::GetInstance()‑>
CanRequestAds() 始终会返回 false如果在意见征求过程中发生错误,请检查您是否可以请求广告。UMP SDK 会使用之前应用会话中的同意情况。
防止重复的广告请求工作
在收集用户意见后以及在调用
RequestConsentInfoUpdate()后,当您检查
ConsentInfo::GetInstance()‑>
CanRequestAds()时,请确保您的逻辑可以防止重复的广告请求,因为这
可能会导致两次检查都返回true。例如,使用布尔变量。
以下完整示例使用了更新循环轮询,但您也可以使用 回调函数
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;
}
}
}
}
测试
如果您希望在应用开发过程中测试集成,请按照以下步骤以程序化方式注册您的测试设备。在发布应用之前,请务必移除用于设置这些测试设备 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)、
英国 (UK) 和瑞士等各个地区一样,使用
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();