How to Retrieve consent status for Forward to the Google Mobile Ads SDK in Flutter

256 Views Asked by At

I have Implemented my app to load consent form for EU economic aria users. Message is displaying correctly. Users can select "do not consent" or "consent" options from that.

enter image description here

But I see in the documentation they are mentioning If a user has consented to receive only non-personalized ads, we need to configure an AdRequest object with the following code to specify that only non-personalized ads should be requested:

final AdRequest = AdRequest(nonPersonalizedAds: true);

But how can we retrieve user selected consent status for forward to the AdRequest.

I am using google_mobile_ads: ^4.0.0 to display ads and also the consent form. Is any way to get selected consent status or save selected status in a shared preferences when user click on the button in consent form.

this is the class created by me to load consent form

import 'package:google_mobile_ads/google_mobile_ads.dart';

class ConsentUpdate
{

  ///Function use to request the latest consent Information---------------------
  requestLatestConsentInformation()
  {

    final params = ConsentRequestParameters(
      consentDebugSettings: ConsentDebugSettings(
        debugGeography: DebugGeography.debugGeographyEea,
        testIdentifiers: ["EDA1C0CE59EF80A7C949F6B128CC96D9"]
      )
    );

    ConsentInformation.instance.requestConsentInfoUpdate(
      params,
      () async
      {
        // The consent information state was updated.
        // You are now ready to check if a form is available.
        if (await ConsentInformation.instance.isConsentFormAvailable())
        {
          loadForm();
        }

      },
      (FormError error)
      {
        // Handle the error
      },
    );
  }
  ///End of function use to request the latest consent information--------------


  ///Function use to load the form----------------------------------------------
  void loadForm()
  {
    ConsentForm.loadConsentForm(
          (ConsentForm consentForm) async
          {
             // Present the form
            var status = await ConsentInformation.instance.getConsentStatus();

            if (status == ConsentStatus.required)
            {
              consentForm.show((formError)
              {
                loadForm(); // Handle dismissal by reloading form
              });
            }

          },
          (FormError formError)
          {
            // Handle the error
          },
    );
  }
  ///End of function use to load the form---------------------------------------


  ///Function use to update consent information---------------------------------
  updateConsentInformation()
  {

      ConsentInformation.instance.requestConsentInfoUpdate(
        ConsentRequestParameters(),
        () async
      {

        if(await ConsentInformation.instance.isConsentFormAvailable())
        {
          ConsentForm.loadConsentForm((consentForm)
          {
            consentForm.show((formError) async
            {

            });
          },
          (formError)
          {

          }
          );
      }

    },
    (error)
    {

    }
    );

  }
  ///End of function use to update consent information--------------------------


}
0

There are 0 best solutions below