Search code examples
androidkotlintestingadmob

How to setup new ConsentDebugSettings.Builder().addTestDeviceHashedId("DEVICE_ID")?


I always used the following implementaion for testing Ads within my app:

MobileAds.initialize(this)
val testDeviceIds = listOf(
    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
     ....

     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)
val configuration = RequestConfiguration
    .Builder()
    .setTestDeviceIds(testDeviceIds)
    .build()
MobileAds.setRequestConfiguration(configuration)

Recently I noticed new suggestion in the logs:

Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") to set this as a debug device.

and I added that to the code:

ConsentDebugSettings.Builder(this)
            .addTestDeviceHashedId("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").build()

But no result, it keeps showing that message again and again.

What have I done wrong?


Solution

  • Use ConsentDebugSettings like this:

    private ConsentInformation consentInformation;
    private ConsentForm consentForm;
    
    private void showConsentForm() {
        ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(this)
                .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
                .addTestDeviceHashedId("your_device_id_from_logcat")
                .build();
    
        ConsentRequestParameters params = new ConsentRequestParameters
                .Builder()
                .setConsentDebugSettings(debugSettings)
                .setTagForUnderAgeOfConsent(false)
                .build();
    
        consentInformation = UserMessagingPlatform.getConsentInformation(this);
        consentInformation.requestConsentInfoUpdate(this, params,
                new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
                    @Override
                    public void onConsentInfoUpdateSuccess() {
                        if (consentInformation.isConsentFormAvailable()) {
                            loadForm();
                        }
                    }
                },
                new ConsentInformation.OnConsentInfoUpdateFailureListener() {
                    @Override
                    public void onConsentInfoUpdateFailure(FormError formError) {
                    }
                });
    }
    
    private void loadForm() {
        UserMessagingPlatform.loadConsentForm(this,
                new UserMessagingPlatform.OnConsentFormLoadSuccessListener() {
                    @Override
                    public void onConsentFormLoadSuccess(ConsentForm consentForm) {
                        this.consentForm = consentForm;
                        if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.REQUIRED) {
                            consentForm.show(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.
                    }
                }
        );
    }