Repository navigation
Privacy and Consent UMP
Google User Messaging Platform (UMP) must run before Google Mobile Ads is initialized or any ad request is made.
App starts
-> refresh consent information
-> load and show a required consent form
-> evaluate CanRequestAds
-> configure TFAT age treatment
-> initialize Mobile Ads
-> load ads
Run the refresh on every app launch. Do not assume that a status cached from a previous launch is still valid.
IAdMobConsentService is registered by UseAMDevITAdMobWrapper() on every
MAUI target.
public sealed class AdMobStartup(IAdMobConsentService consentService)
{
public async Task<bool> InitializeAsync(
CancellationToken cancellationToken = default)
{
if (!consentService.IsSupported)
{
// UMP is not available on this platform. Skip the advertising
// startup and let the application continue with its fallback UI.
return false;
}
bool canRequestAds;
try
{
ConsentGatheringResult result = await consentService.GatherConsentAsync(
cancellationToken: cancellationToken);
canRequestAds = result.CanRequestAds;
}
catch (ConsentException exception) when (exception.CanRequestAds == true)
{
// Refresh failed, but the previous consent state still permits ads.
canRequestAds = true;
}
if (!canRequestAds)
return false;
string applicationId = OperatingSystem.IsAndroid()
? "ca-app-pub-XXXXXXXXXXXXXXXX~YYYYYYYYYY"
: string.Empty;
await consentService.InitializeAsync(applicationId,
AdMobAgeTreatment.Teen,
cancellationToken);
return true;
}
}The application ID argument is required on Android. iOS ignores it and reads
GADApplicationIdentifier from Info.plist.
| Property | Android/iOS | Windows/Mac Catalyst |
|---|---|---|
IsSupported |
true |
false |
CanRequestAds |
Native UMP state | true |
IsInitialized |
Native SDK state | true |
CurrentConsentInformation |
Native snapshot | Neutral NotRequired snapshot |
Desktop methods are safe no-ops and log that consent is unavailable. They do not throw, including when called with an already-cancelled token.
AdMobManager manager = AdMobManager.Instance;
ConsentGatheringResult result = await manager.GatherConsentAsync(this);
if (!result.CanRequestAds)
return;
await manager.InitializeAsync(this.ApplicationContext!,
"ca-app-pub-XXXXXXXXXXXXXXXX~YYYYYYYYYY",
AdMobAgeTreatment.Teen);AdMobManager manager = AdMobManager.Instance;
ConsentGatheringResult result = await manager.GatherConsentAsync(this);
if (!result.CanRequestAds)
return;
await manager.InitializeAsync(this, AdMobAgeTreatment.Teen);When ConsentGatheringResult.PrivacyOptionsRequired is true, expose a
persistent privacy entry point in the application's settings:
await consentService.ShowPrivacyOptionsFormAsync(cancellationToken);You can also inspect:
ConsentInformationSnapshot? snapshot = consentService.CurrentConsentInformation;
bool privacyEntryRequired =
snapshot?.PrivacyOptionsRequirementStatus ==
PrivacyOptionsRequirementStatus.Required;-
UpdateCurrentConsentInformationAsyncrefreshes UMP state without running the combined gather flow. -
LoadAndShowConsentFormIfRequiredAsynchandles only the required form. -
ShowPrivacyOptionsFormAsyncopens the user's privacy controls. -
ResetConsentForTestingclears consent state for development tests.
ConsentRequestOptions options = new(
TagForUnderAgeOfConsent: false,
DebugParameters: new ConsentDebugParameters(
ConsentDebugGeography.Eea,
"TEST-DEVICE-HASH"));
ConsentGatheringResult result = await consentService.GatherConsentAsync(options);Never include debug geography or test-device identifiers in a production build.
The UMP TagForUnderAgeOfConsent option controls consent collection. It does
not configure the age treatment of Mobile Ads requests. Configure TFAT
separately during SDK initialization:
await consentService.InitializeAsync(applicationId,
AdMobAgeTreatment.Teen,
cancellationToken);Available values are:
-
Unspecified: no age-treatment signal; -
Child: child age treatment; -
Teen: teen age treatment, which disables personalized ads and remarketing and enables Google's teen ad-serving protections.
Select the value using your own age-assurance flow and legal requirements. Do
not infer Teen automatically from the UMP under-age flag. The overload without
an AdMobAgeTreatment remains backward compatible and leaves the treatment
unspecified. See Google's
Tag for age treatment guidance.
Configure TFAT and initialize the SDK once before the first ad load. Every banner and full-screen load must happen after initialization has completed; do not initialize again for each ad.
ConsentException exposes:
-
ErrorCodeandMessage; - nullable
CanRequestAdsstate available when the failure occurred; - nullable
PrivacyOptionsRequiredstate.
Only continue loading ads after an error when CanRequestAds == true.