ali
ali

Reputation: 199

GDPR Consent Dialog won't show

i'm trying to add the GDPR Consent dialog to my application by using th new Consent SDK by Google ,(I do not live in the EU) but i can't here's my code when i run it the dialog won't open, i tried to use VPN but still the same dialog doesn't appears

/*GDRP*/
        ConsentInformation consentInformation = ConsentInformation.getInstance(this);
        String[] publisherIds = {"pub-xxxxx...."};
        consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
            @Override
            public void onConsentInfoUpdated(ConsentStatus consentStatus) {
            }
            @Override
            public void onFailedToUpdateConsentInfo(String errorDescription) {

            }
        });
        URL privacyUrl = null;
        try {
            // TODO: Replace with your app's privacy policy URL.
            privacyUrl = new URL("URL");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            // Handle error.
        }
        form = new ConsentForm.Builder(this, privacyUrl)
                .withListener(new ConsentFormListener() {
                    @Override
                    public void onConsentFormLoaded(){
                        showForm();
                    }

                    @Override
                    public void onConsentFormOpened() {
                    }

                    @Override
                    public void onConsentFormClosed(
                            ConsentStatus consentStatus, Boolean userPrefersAdFree) {
                    }

                    @Override
                    public void onConsentFormError(String errorDescription) {
                    }
                })
                .withPersonalizedAdsOption()
                .withNonPersonalizedAdsOption()
                .withAdFreeOption()
                .build();

        form.load();

 private void showForm() {
        form.show();
    }

Upvotes: 2

Views: 2811

Answers (3)

maclir
maclir

Reputation: 3309

As Alexandru Topală mentioned, you can't call show() right after load().

Very poorly documented...

I implemented it with kotlin, and late init:

lateinit var form: ConsentForm
form = ConsentForm.Builder(this@HomeActivity, privacyUrl)
        .withListener(object : ConsentFormListener() {
            override fun onConsentFormLoaded() {
                form.show()
            }

            override fun onConsentFormOpened() {}

            override fun onConsentFormClosed(consentStatus: ConsentStatus, userPrefersAdFree: Boolean) {
                viewModel.consentUpdate(consentStatus)
                if (userPrefersAdFree) {
                    PurchaseActivity.startActivity(this@HomeActivity)
                }
            }

            override fun onConsentFormError(errorDescription: String) {
                Timber.e(RuntimeException(errorDescription), "Failed to open consent form.")
            }
        })
        .withPersonalizedAdsOption()
        .withNonPersonalizedAdsOption()
        .withAdFreeOption()
        .build()
form.load()

Upvotes: 0

Iffat Fatima
Iffat Fatima

Reputation: 1748

Make sure you add this line to test in non-EU region.

ConsentInformation.getInstance(context).
    setDebugGeography(DebugGeography.DEBUG_GEOGRAPHY_EEA);

Upvotes: 0

Alexandru Topală
Alexandru Topală

Reputation: 329

I had the exact same issue. The problem is that the form is not fully loaded when you trying to show it, due to a bug in the SDK.

This should solve it:

 // declare your form up
 ConsentForm form;

 // declare this function that will show the form
 protected void showConsentForm(){
     form.show();
 }


 // on the onCreate 
 form = new ConsentForm.Builder(context, privacyUrl)
            .withListener(new ConsentFormListener() {
                @Override
                public void onConsentFormLoaded() {
                    // Consent form loaded successfully.
                    Log.d("SplashScreen", "Consent form Loaded ");
                    showConsentForm();
                }

                @Override
                public void onConsentFormOpened() {
                    // Consent form was displayed.
                    Log.d("SplashScreen", "Consent form opened ");
                }

                @Override
                public void onConsentFormClosed(
                        ConsentStatus consentStatus, Boolean userPrefersAdFree) {
                    // Consent form was closed.
                    Log.d("SplashScreen", "Consent form Closed ");
                }

                @Override
                public void onConsentFormError(String errorDescription) {
                    // Consent form error.
                    Log.d("SplashScreen", "Consent form error " + errorDescription);
                }
            })
            .withPersonalizedAdsOption()
            .withNonPersonalizedAdsOption()
            .build();
// load the form so we can call .show on it after
form.load();

Upvotes: 7

Related Questions