How to implement AdMob GDPR message in Android application

If you are an Android developer and looking for AdMob GDPR implementation then you are in the right place. In this tutorial, I will share with you how to implement AdMob GDPR Message in an Android application. So let’s start.

Beginning of January 2024 all Android developers need to implement Admob GDPR message to server ads in the European Union and the United Kingdom. Without getting consent from the user no ad will appear in your Android app. So It’s most important for setting up the GDPR Message. Let’s Implement the GDPR message.

Dependency

implementation 'com.google.android.ump:user-messaging-platform:2.1.0'
First, go to your app build. gradle file add this dependency and click to sync button. After successful sync, AdMob UMP SDK will be added in your project. 

JAVA File

Now go to your app activity and select an activity where you want to set up a GDPR message. Declare these two variables outside the OnCreate method.

private ConsentInformation consentInformation;

private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);

Now to go your OnCreate Method and paste this code. The code is given below

 ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(this)
                .addTestDeviceHashedId("36AC515B03D289296D467572BED30BE8")
                .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
                .build();





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

        consentInformation = UserMessagingPlatform.getConsentInformation(this);
        consentInformation.requestConsentInfoUpdate(
                this,
                params,
                (ConsentInformation.OnConsentInfoUpdateSuccessListener) () -> {
                    UserMessagingPlatform.loadAndShowConsentFormIfRequired(
                            this,
                            (ConsentForm.OnConsentFormDismissedListener) loadAndShowError -> {
                                if (loadAndShowError != null) {
                                    // Consent gathering failed.
                                    Log.w(TAG, String.format("%s: %s",
                                            loadAndShowError.getErrorCode(),
                                            loadAndShowError.getMessage()));
                                }

                                // Consent has been gathered.
                                if (consentInformation.canRequestAds()) {
                                    initializeMobileAdsSdk();
                                }
                            }
                    );
                },
                (ConsentInformation.OnConsentInfoUpdateFailureListener) requestConsentError -> {
                    // Consent gathering failed.
                    Log.w(TAG, String.format("%s: %s",
                            requestConsentError.getErrorCode(),
                            requestConsentError.getMessage()));
                });

        // Check if you can initialize the Google Mobile Ads SDK in parallel
        // while checking for new consent information. Consent obtained in
        // the previous session can be used to request ads.
        if (consentInformation.canRequestAds()) {
            initializeMobileAdsSdk();
        }

Before Publishing your app in the Play Store, remove the debug setting.

Now go to outside of OnCreate Method and declare this Method.

 private void initializeMobileAdsSdk() {
        
    }

You have to load your admob ad in this method. All the Processes are shared in my video.

If you have any confusion, Watch my YouTube video. The video link is given below


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *