Google User Messaging Platform (UMP) SDK 是一款隐私权和消息工具,可帮助您管理隐私选项。
前提条件
- Android API 级别 21 或更高级别
创建消息类型
在 AdMob 账号的隐私权和消息 标签页下,使用可用的用户消息类型之一创建用户消息。UMP SDK 会尝试显示根据项目中设置的 AdMob 应用 ID 创建的隐私权消息。(google AdMob后台设置)
集成 SDK
将 Google User Messaging Platform SDK 的依赖项添加到模块的应用级 Gradle 文件(通常为 app/build.gradle)中:
dependencies {
implementation "com.google.android.ump:user-messaging-platform:4.0.0"
}
更改应用的 build.gradle 后,请务必使用 Gradle 文件将项目同步。
添加应用 ID
您可以在 AdMob 界面中找到您的应用 ID。 使用以下代码段将 ID 添加到您的 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 和 ConsentForm 的实例:
private final ConsentInformation consentInformation;
private static ConsentForm mConsentForm;
初始化 ConsentInformation 实例:
if(null == mConsentInformation){
mConsentInformation = UserMessagingPlatform.getConsentInformation(mContext);
}
每次启动应用时,您都应使用 requestConsentInfoUpdate() 请求更新用户的意见征求信息。此请求会检查以下内容:
-
是否需要征得用户同意。例如,用户是首次需要提供意见征求结果,或者之前的意见征求结果已过期。
-
是否需要隐私设置选项入口点。某些隐私权消息要求应用允许用户随时修改其隐私权选项。
//requestConsentInfoUpdate 请求最新的同意信息 mConsentInformation.requestConsentInfoUpdate((Activity) mContext, params, new ConsentInformation.OnConsentInfoUpdateSuccessListener() { @Override public void onConsentInfoUpdateSuccess() { LogD( "onConsentInfoUpdateSuccess: "); // 同意信息更新成功,isConsentFormAvailable 检测表单是否可用 if(mConsentInformation.isConsentFormAvailable()){ LogD(" ~ 表单可用加载并展示 ~ "); //loadAndShowConsentFormIfRequired 加载用户意见征求表单并展示 UserMessagingPlatform.loadAndShowConsentFormIfRequired((Activity) mContext, new ConsentForm.OnConsentFormDismissedListener() { @Override public void onConsentFormDismissed(@Nullable FormError formError) { if (null != formError) { // 表单加载失败或者无法显示 Log.w(LOG_TAG, "loadAndShowError" + String.format("%s: %s", formError.getErrorCode(), formError.getMessage())); } // canRequestAds 检查是否已征得用户同意 Log.e(LOG_TAG, "征得同意" + mConsentInformation.canRequestAds()); } }); } else { Log.d(LOG_TAG, "onConsentInfoUpdate: 失败" + mConsentInformation.getConsentStatus()); } } }, new ConsentInformation.OnConsentInfoUpdateFailureListener() { @Override public void onConsentInfoUpdateFailure(@NonNull FormError formError) { // 当同意信息更新失败时调用。 if(null != formError){ Log.w(LOG_TAG, "presentForm requestConsentInfoUpdate--->"+String.format("%s: %s", formError.getErrorCode(), formError.getMessage())); } } });
加载并显示隐私权消息表单
在收到最新的用户同意情况后,请调用 loadAndShowConsentFormIfRequired() 以加载收集用户同意情况所需的任何表单。加载后,表单会立即显示。
LogD(" ~ 表单可用加载并展示 ~ ");
//loadAndShowConsentFormIfRequired 加载用户意见征求表单并展示
UserMessagingPlatform.loadAndShowConsentFormIfRequired((Activity) mContext, new ConsentForm.OnConsentFormDismissedListener() {
@Override
public void onConsentFormDismissed(@Nullable FormError formError) {
if (null != formError) {
// 表单加载失败或者无法显示
Log.w(LOG_TAG, "loadAndShowError" + String.format("%s: %s",
formError.getErrorCode(),
formError.getMessage()));
}
// canRequestAds 检查是否已征得用户同意
Log.e(LOG_TAG, "征得同意" + mConsentInformation.canRequestAds());
}
});
检查是否需要隐私选项入口点
调用 requestConsentInfoUpdate() 后,请检查 getPrivacyOptionsRequirementStatus() 以确定您的应用是否需要隐私选项入口点。如果需要,请向应用添加一个可见且可互动的界面元素,用于显示隐私选项表单。如果不需要隐私权入口点,请将界面元素配置为不可见且不可互动。
/** Helper variable to determine if the privacy options form is required. */
public boolean isPrivacyOptionsRequired() {
return consentInformation.getPrivacyOptionsRequirementStatus()
== PrivacyOptionsRequirementStatus.REQUIRED;
}
如需查看隐私权选项要求状态的完整列表,请参阅 ConsentInformation.PrivacyOptionsRequirementStatus。
显示隐私设置选项表单
当用户与您的元素互动时,显示隐私选项表单:
UserMessagingPlatform.showPrivacyOptionsForm(activity, onConsentFormDismissedListener);
在征得用户同意后请求广告
在请求广告之前,请使用 canRequestAds() 检查您是否已征得用户同意:
consentInformation.canRequestAds();
以下列出了在征得用户同意的同时,您可以检查是否可以请求广告的位置:
- 在 UMP SDK 在当前会话中征得用户同意后。
- 在您调用 requestConsentInfoUpdate() 后立即调用。UMP SDK 可能已在之前的应用会话中征得用户同意。
重要提示 : 在您调用 requestConsentInfoUpdate() 之前, canRequestAds() 始终返回 false
防止出现冗余的广告请求工作
在征得用户同意后以及在调用 requestConsentInfoUpdate() 后检查 canRequestAds() 时,请确保您的逻辑可防止冗余的广告请求,因为这些请求可能会导致两次检查都返回 true。例如,使用布尔值变量。
测试
如果您希望在应用开发过程中测试集成,请按照以下步骤以编程方式注册您的测试设备。在发布应用之前,请务必移除用于设置这些测试设备 ID 的代码。
注意: 自版本 2.2.0 起,无需将模拟器添加到您的设备 ID 列表中,因为模拟器默认就是测试设备。检1. 查日志输出,以查找类似于以下示例的消息,其中会显示您的设备 ID 以及如何将设备添加为测试设备:
Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("33BE2250B43518CCDA7DE426D04EE231") to set this as a debug device.
-
将测试设备 ID 复制到剪贴板。
-
修改代码,以便调用
ConsentDebugSettings.Builder().TestDeviceHashedIds并传入您的测试设备 ID 列表。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,
// ...
);