记录 google UMP (User Messaging Platform ) SDK 接入

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

1.google 后台创建消息类型,我们这边一般由发行负责。
2.在 app/build.gradle 配置 ump sdk
dependencies {
  implementation("com.google.android.ump:user-messaging-platform:2.2.0")
}
3.官网要求每次启动应用时使用requestConsentInfoUpdate() 请求更新用户的意见征求信息。我放在 MainActivity 的 onCreate 生命周期中调用。

下面代码中包含了在开发过程中测试应用中的集成。

检查日志输出中是否存在类似如下的消息,该消息显示了您的设备 ID 添加为测试设备:

Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("33BE2250B43518CCDA7DE426D04EE231") to set this as a debug device.
mainfest 中配置
       <!--google admob GDPR 配置 用于开发测试,true 开启测试 、 false 关闭测试-->
        <meta-data
            android:name="GDPR_TEST"
            android:value="false" />
        <!--测试设备哈希id-->
        <meta-data
            android:name="TestDeviceHashedId"
            android:value="EFACB**************D30B0A7B3" />
 
  MainActivity 的 onCreate 声明周期中调用
  public void presentForm() {
        LogD("~ UMP presentForm ~");
        //测试开关
        Boolean testSwitch = false;
        //测试设备哈希ID
        String testDeviceHashedId = "";
        try {
            ApplicationInfo appInfo = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(),
                    PackageManager.GET_META_DATA);
            testSwitch = appInfo.metaData.getBoolean("GDPR_TEST", false);
            testDeviceHashedId = appInfo.metaData.getString("TestDeviceHashedId");
        }catch (Exception e){
           e.printStackTrace();
        }
        Log.d(LOG_TAG, "presentForm  testSwitch = " + testSwitch);
        Log.d(LOG_TAG, "presentForm  testDeviceHashedId = " + testDeviceHashedId);
        ConsentRequestParameters params;
        if (testSwitch) {
            //  用于调试
            ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(mContext)
                    .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)//设置地理位置设备位于欧洲经济区 (EEA) 或英国
                    .addTestDeviceHashedId(testDeviceHashedId)//测试设备的哈希 ID
                    .build();
            params = new ConsentRequestParameters
                    .Builder()
                    .setTagForUnderAgeOfConsent(false)
                    .setConsentDebugSettings(debugSettings)//设置设备的哈希 ID 用于调试
                    .build();
        } else {
            params = new ConsentRequestParameters
                    .Builder()
                    .setTagForUnderAgeOfConsent(false)
                    .build();
        }

        if(null == mConsentInformation){
            mConsentInformation = UserMessagingPlatform.getConsentInformation(mContext);
        }
    
        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()));
                            }

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

    }

注意: 在发布应用之前,请务必关闭测试开关,移除设置的测试设备 ID 的代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

遥不可及zzz

我会用心写更多自己的经验。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值