You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何获取用户同意状态并传递给Google Mobile Ads SDK

解决方案

一、获取用户同意状态并配置AdRequest

你可以通过ConsentInformation类获取用户的最终同意状态,以此决定是否启用非个性化广告:

  1. 获取同意状态:在用户提交同意表单后,调用await ConsentInformation.instance.getConsentStatus()获取当前状态,对应ConsentStatus的三种枚举值:

    • ConsentStatus.required:用户尚未提交同意选择
    • ConsentStatus.notRequired:用户所在地区无需同意(非欧盟经济区)
    • ConsentStatus.obtained:用户已提交同意选择
  2. 判断非个性化广告配置:

    • 当用户选择「仅同意非个性化广告」时,将AdRequest的nonPersonalizedAds设为true
    • 若用户同意个性化广告,设为false(默认值为false,可省略配置)

二、用SharedPreferences保存同意状态

完全可以将用户的同意状态保存到SharedPreferences中,APP重启后无需重复请求同意信息,直接读取本地缓存即可。

步骤1:添加依赖

在pubspec.yaml中补充shared_preferences依赖:

dependencies:
  flutter:
    sdk: flutter
  google_mobile_ads: ^4.0.0
  shared_preferences: ^2.2.2

步骤2:修改ConsentUpdate类,加入状态管理逻辑

更新你的ConsentUpdate类,添加保存、读取同意状态的方法,并在用户提交表单后自动保存状态:

import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ConsentUpdate {
  /// 从本地缓存读取是否启用非个性化广告
  Future<bool> getNonPersonalizedAdsStatus() async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.getBool('non_personalized_ads') ?? false;
  }

  /// 保存同意状态到本地缓存
  Future<void> saveConsentStatus() async {
    final consentStatus = await ConsentInformation.instance.getConsentStatus();
    final prefs = await SharedPreferences.getInstance();
    
    // 保存同意状态枚举索引
    await prefs.setInt('consent_status', consentStatus.index);
    
    // 判断是否需要启用非个性化广告
    bool isNonPersonalized = false;
    if (consentStatus == ConsentStatus.obtained) {
      // 直接通过官方API判断用户是否授权个性化广告
      isNonPersonalized = !await ConsentInformation.instance.isPersonalizedAdsEnabled();
    }
    await prefs.setBool('non_personalized_ads', isNonPersonalized);
  }

  /// 请求最新同意信息
  void requestLatestConsentInformation() {
    final params = ConsentRequestParameters(
      consentDebugSettings: ConsentDebugSettings(
        debugGeography: DebugGeography.debugGeographyEea,
        testIdentifiers: ["EDA1C0CE59EF80A7C949F6B128CC96D9"]
      )
    );

    ConsentInformation.instance.requestConsentInfoUpdate(
      params,
      () async {
        if (await ConsentInformation.instance.isConsentFormAvailable()) {
          loadForm();
        } else {
          await saveConsentStatus();
        }
      },
      (FormError error) {
        // 处理请求错误
      },
    );
  }

  /// 加载并展示同意表单
  void loadForm() {
    ConsentForm.loadConsentForm(
      (ConsentForm consentForm) async {
        final status = await ConsentInformation.instance.getConsentStatus();
        if (status == ConsentStatus.required) {
          consentForm.show((formError) async {
            if (formError == null) {
              // 用户提交同意后保存状态
              await saveConsentStatus();
            } else {
              // 表单展示错误,重新加载
              loadForm();
            }
          });
        } else {
          await saveConsentStatus();
        }
      },
      (FormError formError) {
        // 处理表单加载错误
      },
    );
  }

  /// 更新同意信息
  void updateConsentInformation() {
    ConsentInformation.instance.requestConsentInfoUpdate(
      ConsentRequestParameters(),
      () async {
        if (await ConsentInformation.instance.isConsentFormAvailable()) {
          ConsentForm.loadConsentForm((consentForm) {
            consentForm.show((formError) async {
              if (formError == null) {
                await saveConsentStatus();
              }
            });
          }, (formError) {
            // 处理错误
          });
        } else {
          await saveConsentStatus();
        }
      },
      (error) {
        // 处理请求错误
      },
    );
  }
}

三、创建AdRequest时读取缓存状态

在需要加载广告的业务代码中,读取本地缓存的状态并创建AdRequest:

final consentUpdate = ConsentUpdate();
final nonPersonalizedAds = await consentUpdate.getNonPersonalizedAdsStatus();
final adRequest = AdRequest(nonPersonalizedAds: nonPersonalizedAds);

关键说明

  • isPersonalizedAdsEnabled()是Google Mobile Ads提供的官方API,可直接判断用户是否授权个性化广告,无需额外解析表单选项
  • 保存consent_status可用于后续判断是否需要重新触发同意流程(如用户切换地区场景)
  • 调试阶段可通过ConsentDebugSettings模拟欧盟地区用户,覆盖不同同意场景测试

内容的提问来源于stack exchange,提问作者Sampath Dissanayake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 01:21:23