Rewind
Rewind

Reputation: 2814

Implementing TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE and MAX_AD_CONTENT_RATING_G

[Edit for clarity: Have I got the RequestConfiguration requestConfiguration = in the right place? Or should it go before ConsentRequestParameters params = new ConsentRequestParameters or somewhere else?]

I want to merge some code into mine so I am showing kid friendly adverts in my app.

So I (think) I have found I need to have TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE and MAX_AD_CONTENT_RATING_G in my code somewhere.

For the GDPR stuff I have the code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // GDPR
    ConsentRequestParameters params = new ConsentRequestParameters
            .Builder()
            .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()));
                boolean t = true;
            });
    // 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();
    }

    ...
    ...
}

private void initializeMobileAdsSdk() {
    if (isMobileAdsInitializeCalled.getAndSet(true)) {
        return;
    }

    // Initialize the Google Mobile Ads SDK.
    //m_AdB = new MyBannerAd(this);
    m_AdI = new MyInterstitialAd(this);

    // RewardedAd
    //m_AdR = new MyRewardedAd(this); This did not work mut be done on main thread
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });
    m_AdI.RequestAd();
}

Am I right in believing I need to change my GDPR stuff to the following? (I am a bit concerned if I implement this and I am wrong I will not know until I get a strike against me.):

private void initializeMobileAdsSdk() {
    if (isMobileAdsInitializeCalled.getAndSet(true)) {
        return;
    }

    // NEW STUFF
    // NEW STUFF
    // NEW STUFF
    //AdMob Apps for everyone, including children and families policy
    RequestConfiguration requestConfiguration = 
    MobileAds.getRequestConfiguration()
        .toBuilder()
        .setTagForChildDirectedTreatment(
                RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE)
        .setMaxAdContentRating(RequestConfiguration.MAX_AD_CONTENT_RATING_G)
        .build();
    MobileAds.setRequestConfiguration(requestConfiguration);
    // END OF NEW STUFF
    // END OF NEW STUFF
    // END OF NEW STUFF

    // Initialize the Google Mobile Ads SDK.
    m_AdI = new MyInterstitialAd(this);

    // RewardedAd
    //m_AdR = new MyRewardedAd(this); This did not work mut be done on main thread
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });
    m_AdI.RequestAd();
}

... ... ...

If this is important, my other advert code looks like this:

void RequestAd(){
    AdRequest adRequest = new AdRequest.Builder().build();
    InterstitialAd.load(m_Context, interStitialID, adRequest,
        new InterstitialAdLoadCallback() {
            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                // Handle the error
                mInterstitialAd = null;
                // My code
                RequestAd();
            }
            @Override
            public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                // The mInterstitialAd reference will be null until
                // an ad is loaded.
                mInterstitialAd = interstitialAd;

                mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
                    @Override
                    public void onAdClicked() {
                        // Called when a click is recorded for an ad.

                        // My code
                        mInterstitialAd = null;
                        RequestAd();
                    }

                    @Override
                    public void onAdDismissedFullScreenContent() {
                        // Called when ad is dismissed.
                        // Set the ad reference to null so you don't show the ad a second time.
                        mInterstitialAd = null;

                        // My code
                        RequestAd();
                    }

                    @Override
                    public void onAdFailedToShowFullScreenContent(AdError adError) {
                        // Called when ad fails to show.
                        mInterstitialAd = null;

                        // My code
                        RequestAd();
                    }

                    @Override
                    public void onAdImpression() {
                        // Called when an impression is recorded for an ad.
                    }

                    @Override
                    public void onAdShowedFullScreenContent() {
                        // Called when ad is shown.
                    }
                });
            }
        });
}

public void ShowAd(){
    long milliNow = System.currentTimeMillis();
    boolean showAd = (milliBetweenInterstitialAd > 0 &&
                    milliNow >= (milliBetweenInterstitialAd + milliLastInterstitialAd))
            ? true : false;
    if (showAd && mInterstitialAd != null) {
        milliLastInterstitialAd = milliNow;
        //mInterstitialAd.show(MainActivity.this);
        mInterstitialAd.show(m_Context);
    } else {
    }
}

Upvotes: 1

Views: 60

Answers (0)

Related Questions