2

Currently I am using following code to show banner ads in my android app

 private ConsentForm form;
 private AdView abAdView;

  private void checkForConsent() {

    try {



        ConsentInformation consentInformation = ConsentInformation.getInstance(FirstActivity.this);
        ConsentStatus consentStatus = consentInformation.getConsentStatus();
        try {
            if (consentStatus == ConsentStatus.NON_PERSONALIZED) {

                showNonPersonalizedAds();

            } else if (consentStatus == ConsentStatus.PERSONALIZED) {
                showPersonalizedAds();
            } else {
                String[] publisherIds = {"my publisher id"};
                consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
                    @Override
                    public void onConsentInfoUpdated(ConsentStatus consentStatus) {
                        // User's consent status successfully updated.
                        switch (consentStatus) {
                            case PERSONALIZED:
                                showPersonalizedAds();
                                break;
                            case NON_PERSONALIZED:
                                showNonPersonalizedAds();
                                break;
                            case UNKNOWN:
                                if (ConsentInformation.getInstance(getBaseContext())
                                        .isRequestLocationInEeaOrUnknown()) {
                                    requestConsent();
                                } else {
                                    showPersonalizedAds();
                                }
                                break;
                            default:
                                break;
                        }
                    }

                    @Override
                    public void onFailedToUpdateConsentInfo(String errorDescription) {
                        // User's consent status failed to update.
                    }
                });
            }
        } catch (Exception e) {
            String[] publisherIds = {"my publisher id"};
            consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
                @Override
                public void onConsentInfoUpdated(ConsentStatus consentStatus) {
                    // User's consent status successfully updated.
                    switch (consentStatus) {
                        case PERSONALIZED:
                            showPersonalizedAds();
                            break;
                        case NON_PERSONALIZED:
                            showNonPersonalizedAds();
                            break;
                        case UNKNOWN:
                            if (ConsentInformation.getInstance(getBaseContext())
                                    .isRequestLocationInEeaOrUnknown()) {
                                requestConsent();
                            } else {
                                showPersonalizedAds();
                            }
                            break;
                        default:
                            break;
                    }
                }

                @Override
                public void onFailedToUpdateConsentInfo(String errorDescription) {
                    // User's consent status failed to update.
                }
            });
        }

    }catch (Exception e)
    {
        try {
            if (abAdView != null) {
                abAdView.pause();
                adContainerView.removeAllViews();
                abAdView.destroy();
                abAdView = null;
            }
        }catch (Exception ignored){}
    }

}

private void requestConsent() {
    URL privacyUrl = null;
    try {
        privacyUrl = new URL("my privacy URL");
    } catch (MalformedURLException ignored) {

    }
    form = new ConsentForm.Builder(FirstActivity.this, privacyUrl)
            .withListener(new ConsentFormListener() {
                @Override
                public void onConsentFormLoaded() {
                    // Consent form loaded successfully.
                    showForm();
                }

                @Override
                public void onConsentFormOpened() {
                    // Consent form was displayed.
                }

                @Override
                public void onConsentFormClosed(
                        ConsentStatus consentStatus, Boolean userPrefersAdFree) {
                    switch (consentStatus) {
                        case PERSONALIZED:
                        {showPersonalizedAds();break;}
                        case NON_PERSONALIZED:
                        case UNKNOWN:
                        {showNonPersonalizedAds();break;}
                    }
                    // Consent form was closed.
                }

                @Override
                public void onConsentFormError(String errorDescription) {

                    // Consent form error.
                }
            })
            .withPersonalizedAdsOption()
            .withNonPersonalizedAdsOption()
            .build();
    form.load();
}


private void showPersonalizedAds() {
    try {
        abAdView = new AdView(FirstActivity.this);
        abAdView.setAdUnitId("my ad unit id");
        adContainerView.removeAllViews();
        adContainerView.addView(abAdView);

        // first of all get ad size
        
        AdSize adSize = getAdSize();
        abAdView.setAdSize(adSize);

        //banner ad
        MobileAds.initialize(FirstActivity.this);


        // Step 1 - Create an AdView and set the ad unit ID on it.
        ConsentInformation.getInstance(FirstActivity.this)
                .setConsentStatus(ConsentStatus.PERSONALIZED);

        AdRequest adRequest = new AdRequest.Builder()
                .build();


        abAdView.loadAd(adRequest);

    }catch (Exception e)
    {
        try {
            if (abAdView != null) {
                abAdView.pause();
                adContainerView.removeAllViews();
                abAdView.destroy();
                abAdView = null;
            }
        }catch (Exception ignored){}
    }

}

private void showNonPersonalizedAds() {
    try{

        abAdView = new AdView(FirstActivity.this);
        abAdView.setAdUnitId("my ad unit id");
        adContainerView.removeAllViews();
        adContainerView.addView(abAdView);

        //first of all get ad size

        AdSize adSize = getAdSize();
        abAdView.setAdSize(adSize);

        //banner ad
        MobileAds.initialize(FirstActivity .this);


        ConsentInformation.getInstance(FirstActivity .this)
                .setConsentStatus(ConsentStatus.NON_PERSONALIZED);


        AdRequest adRequest = new AdRequest.Builder()
                .addNetworkExtrasBundle(AdMobAdapter.class, getNonPersonalizedAdsBundle())
                .build();

        abAdView.loadAd(adRequest);

    }catch (Exception e)
    {
        try {
            if (abAdView != null) {
                abAdView.pause();
                adContainerView.removeAllViews();
                abAdView.destroy();
                abAdView = null;
            }
        }catch (Exception ignored){}
    }

}


private AdSize getAdSize() {
    // Determine the screen width (less decorations)
    // to use for the ad width.
    
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    float density = outMetrics.density;

    float adWidthPixels = adContainerView.getWidth();

    // If the ad hasn't been laid out,
   // default to the full screen width.

    if (adWidthPixels == 0) {
        adWidthPixels = outMetrics.widthPixels;
    }

    int adWidth = (int) (adWidthPixels / density);

     return AdSize.
            getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 
            adWidth);
}


private Bundle getNonPersonalizedAdsBundle() {
    Bundle extras = new Bundle();
    extras.putString("npa", "1");

    return extras;
}

private void showForm() {
    if (form != null) {
        form.show();
    }
}

This code is based on: https://developers.google.com/admob/android/eu-consent

but now it seems that this whole code is deprecated with replacement: https://developers.google.com/admob/ump/android/quick-start

So how to replace this deprecated code? Is this deprecated am I right? If it is deprecated then what are similar functions for obtaining consent from European users?

Chaitanya Karmarkar
  • 644
  • 1
  • 3
  • 14

1 Answers1

1

Yesterday I run into this too. First of all you must replace the library with the new one and follow all the steps in the quick-start. Remember to follow the pre-requisites, particularly the creation of FundingChoices on your adMob settings. Your code is almost good.

Phantom Lord
  • 361
  • 2
  • 10
  • Just one more query, https://developers.google.com/admob/ump/android/quick-start this page contains one note at top. Important: Google is planning to launch its full support of the IAB TCFv2 for GDPR on August 15, 2020. We are releasing the Funding Choices IAB solution early to ensure you have adequate time to set up and test your consent messages on your apps prior to this launch. Prior to August 15, publishers should not publish consent messages using the IAB framework to apps with live traffic. So does this means that till 15 august 2020 we should not publish updated app version on store? – Chaitanya Karmarkar Jul 26 '20 at 12:24
  • Does this means before 15 august 2020 we should prepare our code, test it and after 15 aug 2020 we can publish? – Chaitanya Karmarkar Jul 26 '20 at 12:28
  • 2
    yes, I am testing with test ads and.... the test ads is displayed well. The consent instead don't update. If I update with consent or don't consent through the form, it always return "Non personalized ads". I have made a post on the support forum page: https://groups.google.com/g/google-admob-ads-sdk/c/ba0vdGccxQY – Phantom Lord Jul 26 '20 at 16:14