Implement Admob GDPR message - Java

Complete the following steps to create a GDPR message. 

  1. Library Implement (Admob & GDPR )
  2. Create GDPR Java class 
  3. Call this method in your activity class

1. Library Implement (Admob & GDPR )


Code:
    //GDPR Message
    implementation 'com.google.android.ump:user-messaging-platform:2.1.0' 

 2. Create GDPR Java class


Code :

public class GDPR {
    Activity activity;
    String GDPR_DEBUG_TAG = "";
    private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);

    public GDPR(Activity activity) {
        this.activity = activity;
    }

    public void setGDPR() {
        ConsentRequestParameters params = new ConsentRequestParameters
                .Builder()
                .setTagForUnderAgeOfConsent(false)
                .build();

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

                        // Consent has been gathered.
                        if (consentInformation.canRequestAds()) {
                            initializeMobileAdsSdk();
                        }
                    }
            );

        }, (ConsentInformation.OnConsentInfoUpdateFailureListener) requestConsentError -> {
            Log.w(GDPR_DEBUG_TAG, String.format("%s: %s", requestConsentError.getErrorCode(), requestConsentError.getMessage()));
        });

        if (consentInformation.canRequestAds()) {
            initializeMobileAdsSdk();
        }

    } // setGDPR End here ======================

    private void initializeMobileAdsSdk() {
        if (isMobileAdsInitializeCalled.getAndSet(true)) {
            return;
        }
        MobileAds.initialize(activity);
    } // initializeMobileAdsSdk end here =========

} // GDPR End Here ===============================  

3. Call this method in your activity class


Code:
    // Create GDPR Class Object and call setGDPR method
    GDPR gdpr = new GDPR(MainActivity.this);
    gdpr.setGDPR();

You Are Successfully  Implement GDPR Message 😍
Next Post Previous Post
3 Comments
  • AL QURANER AHOBAN
    AL QURANER AHOBAN October 13, 2023 at 6:25 PM

    Masha allah

  • YEASIN MIA
    YEASIN MIA October 23, 2023 at 9:54 PM

    bai manifaste kon kico ki add korte hobe

  • Abdul
    Abdul October 25, 2023 at 10:13 AM

    thank you

Add Comment
comment url