开始使用

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

根据 Google 《欧盟地区用户意见征求政策》,您必须向欧洲经济区 (EEA) 内的用户以及英国披露相关信息;在法律有相应要求的情况下,必须征得他们的同意,才能使用 Cookie 或其他本地存储方式,也不得使用个人数据(例如 AdID)来投放广告。此政策反映了欧盟《电子隐私指令》和《一般数据保护条例》(GDPR) 的要求。

为了帮助发布商履行此政策规定的职责,Google 提供了 User Messaging Platform (UMP) SDK。UMP SDK 已更新为支持最新的 IAB 标准。现在,您可以在隐私权和消息中 AdMob 处理所有此类配置。

前提条件

用户消息类型

请参阅 用户消息类型 查看受支持消息的完整列表。有关实现每种消息类型的具体说明,请参阅左侧导航栏。

使用 Gradle 进行安装

如果您使用的是 Google 移动广告 SDK 19.8.0 或更高版本,您的软件包中会包含 UMP SDK。如果您使用的是早期版本的移动广告 SDK,请按以下方式将 UMP SDK 添加到应用的 build.gradle 中:

dependencies {
    // This dependency is automatically included by Google Mobile Ads SDK 19.8.0
    // or higher.
    implementation 'com.google.android.ump:user-messaging-platform:2.0.0'
}

更改应用的 build.gradle 后,请务必将项目与 Gradle 文件同步。

接下来, 查找您的应用 ID 并将其添加到您的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rewardedinterstitialexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <!-- Sample app ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

确定是否需要显示消息

您应在每次启动时使用 requestConsentInfoUpdate() 请求更新用户的同意信息。这可以确定您的用户是否需要提供同意声明(他们尚未提供或者需要同意)。

根据需要演示表单时使用存储在 consentInformation对象中的信息。

以下示例展示了如何在应用启动时检查状态:

Java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.google.android.ump.ConsentForm;
import com.google.android.ump.ConsentInformation;
import com.google.android.ump.ConsentRequestParameters;
import com.google.android.ump.FormError;
import com.google.android.ump.UserMessagingPlatform;

public class MainActivity extends AppCompatActivity {
    private ConsentInformation consentInformation;
    private ConsentForm consentForm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set tag for under age of consent. false means users are not under
        // age.
        ConsentRequestParameters params = new ConsentRequestParameters
            .Builder()
            .setTagForUnderAgeOfConsent(false)
            .build();

        consentInformation = UserMessagingPlatform.getConsentInformation(this);
        consentInformation.requestConsentInfoUpdate(
            this,
            params,
            new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
                @Override
                public void onConsentInfoUpdateSuccess() {
                    // The consent information state was updated.
                    // You are now ready to check if a form is available.
                }
            },
            new ConsentInformation.OnConsentInfoUpdateFailureListener() {
                @Override
                public void onConsentInfoUpdateFailure(FormError formError) {
                    // Handle the error.
                }
            });
    }
}

Kotlin

Coming soon.

加载表单(如果有)

在显示表单之前,您需要先确定是否有表单。 不可用的表单可能是由于用户启用了有限的广告跟踪功能,或者您已将这些用户标记为未达到同意年龄。

如需检查表单是否可用,请使用您之前创建的the isConsentFormAvailable() method on the ConsentInformation instance 。

然后,添加一个封装容器方法来加载表单:

Java

...
       consentInformation.requestConsentInfoUpdate(
           this,
           params,
           new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
               @Override
               public void onConsentInfoUpdateSuccess() {
                   // The consent information state was updated.
                   // You are now ready to check if a form is available.
                   if (consentInformation.isConsentFormAvailable()) {
                       loadForm();
                   }
               }
           },
           new ConsentInformation.OnConsentInfoUpdateFailureListener() {
               @Override
               public void onConsentInfoUpdateFailure(FormError formError) {
                   // Handle the error.

               }
            });
    }

    public void loadForm() {

    }
}

Kotlin

Coming soon.

如需加载表单,请使用 the static loadConsentForm() method on the UserMessagingPlatform class。

Java

public void loadForm() {
    // Loads a consent form. Must be called on the main thread.
    UserMessagingPlatform.loadConsentForm(
        this,
        new UserMessagingPlatform.OnConsentFormLoadSuccessListener() {
            @Override
            public void onConsentFormLoadSuccess(ConsentForm consentForm) {
                MainActivity.this.consentForm = consentForm;
            }
        },
        new UserMessagingPlatform.OnConsentFormLoadFailureListener() {
            @Override
            public void onConsentFormLoadFailure(FormError formError) {
                // Handle the error
            }
        }
    );
}

Kotlin

Coming soon.

根据需要展示表单

确定表单的可用性并加载后,使用ConsentForm 实例上的show() 方法来呈现表单。

使用前面的consentInformation 对象检查consent status 并更新loadForm() 方法:

Java

public void loadForm() {
    UserMessagingPlatform.loadConsentForm(
        this,
        new UserMessagingPlatform.OnConsentFormLoadSuccessListener() {
            @Override
            public void onConsentFormLoadSuccess(ConsentForm consentForm) {
                MainActivity.this.consentForm = consentForm;
                if(consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.REQUIRED) {
                    consentForm.show(
                        MainActivity.this,
                            new ConsentForm.OnConsentFormDismissedListener() {
                                @Override
                                public void onConsentFormDismissed(@Nullable FormError formError) {
                                    // Handle dismissal by reloading form.
                                    loadForm();
                                }
                            });

                }

            }
        },
        new UserMessagingPlatform.OnConsentFormLoadFailureListener() {
            @Override
            public void onConsentFormLoadFailure(FormError formError) {
                /// Handle Error.
            }
        }
   );
}

Kotlin

Coming soon.

如果您需要在用户做出选择或关闭表单后执行任何操作,请将该逻辑放入表单的完成处理程序或回调中。

测试

强制设置地理位置

UMP SDK 提供了一种使用 the setDebugGeography method on ConsentDebugSettings.Builder测试应用的行为,就像设备位于欧洲经济区 (EEA) 或英国一样。

您必须在应用的调试设置中提供测试设备的经过哈希处理的 ID,才能使用调试功能。如果您调用requestConsentInfoUpdate() 而不设置此值,应用会在运行时记录所需的 ID 哈希。

Java

ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(this)
    .setDebugGeography(ConsentDebugSettings
    .DebugGeography
    .DEBUG_GEOGRAPHY_EEA)
    .addTestDeviceHashedId("TEST-DEVICE-HASHED-ID")
    .build();

ConsentRequestParameters params = new ConsentRequestParameters
    .Builder()
    .setConsentDebugSettings(debugSettings)
    .build();

consentInformation = UserMessagingPlatform.getConsentInformation(this);
consentInformation.requestConsentInfoUpdate(this, params,
    new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
        @Override
        public void onConsentInfoUpdateSuccess() {
            // The consent information state was updated.
            // You are now ready to check if a form is available.
        }
    },
    new ConsentInformation.OnConsentInfoUpdateFailureListener() {
        @Override
        public void onConsentInfoUpdateFailure(FormError formError) {
            // Handle the error.
        }
    });

Kotlin

Coming soon.

借助 DebugGeography,您可以选择将地理位置强制设置为以下某个选项:

调试地理位置 说明
DEBUG_GEOGRAPHY_DISABLED 调试地理位置已停用。
DEBUG_GEOGRAPHY_EEA 对于调试设备,地理位置在 EEA 中显示。
DEBUG_GEOGRAPHY_NOT_EEA 对于调试设备,地理位置显示为不在欧洲经济区 (EEA) 内。

请注意,调试设置仅适用于测试设备。模拟器无需添加到您的设备 ID 列表中,因为它们默认启用了测试。

使用 UMP SDK 测试应用时,您会发现重置 SDK 的状态会很有帮助,这样您可以模拟用户的首次安装体验。SDK 提供了 reset() 方法来实现此目的。

Java

consentInformation.reset();

Kotlin

consentInformation.reset()

如果您决定将 UMP SDK 从项目中完全移除,您也应调用 reset()