Flutter广告管理插件sourcepoint_unified_cmp_android的使用

Flutter广告管理插件sourcepoint_unified_cmp_android的使用

sourcepoint_unified_cmp_android #

sourcepoint_unified_cmp_android workflow

style: very_good_analysis

这是sourcepoint_unified_cmp的Android实现。

使用 #

此包是被支持的(endorsed),这意味着你可以直接正常使用sourcepoint_unified_cmp。当你这样做时,这个包会自动包含在你的应用中。

完整示例Demo

以下是一个完整的Flutter应用示例,展示了如何使用sourcepoint_unified_cmp插件来管理广告。

首先,在你的pubspec.yaml文件中添加依赖项:

dependencies:
  flutter:
    sdk: flutter
  sourcepoint_unified_cmp: ^1.0.0

然后,确保你已经在Android项目中正确配置了该插件。你需要在android/app/src/main/java/<your_package_name>/MainActivity.javaandroid/app/src/main/kotlin/<your_package_name>/MainActivity.kt文件中添加必要的初始化代码。

接下来,在你的Dart代码中初始化并使用该插件:

import 'package:flutter/material.dart';
import 'package:sourcepoint_unified_cmp/sourcepoint_unified_cmp.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isConsentGiven = false;

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    try {
      // 初始化Sourcepoint CMP
      await SourcepointCMP.init(
        // 替换为你的Sourcepoint CMP配置ID
        configId: 'YOUR_CONFIG_ID',
        // 替换为你的CMP类型
        cmpType: CmpType.sourcepoint,
        // 设置调试模式
        debug: true,
        // 设置语言
        language: 'en',
        // 设置GDPR适用地区
        gdprApplies: true,
      );

      // 请求用户同意
      await SourcepointCMP.requestConsent();

      // 监听用户同意状态变化
      SourcepointCMP.onConsentChanged.listen((consentStatus) {
        setState(() {
          _isConsentGiven = consentStatus == ConsentStatus.granted;
        });
      });
    } catch (e) {
      print('Error initializing Sourcepoint CMP: $e');
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Sourcepoint CMP Example'),
        ),
        body: Center(
          child: Text(_isConsentGiven ? 'User has given consent' : 'User has not given consent'),
        ),
      ),
    );
  }
}

更多关于Flutter广告管理插件sourcepoint_unified_cmp_android的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter广告管理插件sourcepoint_unified_cmp_android的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用sourcepoint_unified_cmp_android插件的示例代码。这个插件用于管理广告,尤其是符合GDPR和其他隐私法规的广告请求。

首先,确保你已经在pubspec.yaml文件中添加了sourcepoint_unified_cmp_android依赖:

dependencies:
  flutter:
    sdk: flutter
  sourcepoint_unified_cmp_android: ^最新版本号  # 请替换为实际最新版本号

然后,运行flutter pub get来安装依赖。

初始化插件

在你的Flutter应用的入口文件(通常是main.dart)中,你需要初始化插件并进行必要的配置。

import 'package:flutter/material.dart';
import 'package:sourcepoint_unified_cmp_android/sourcepoint_unified_cmp_android.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  SourcepointUnifiedCmpAndroid? sourcepoint;

  @override
  void initState() {
    super.initState();
    initSourcepoint();
  }

  void initSourcepoint() async {
    sourcepoint = SourcepointUnifiedCmpAndroid();

    // 配置Sourcepoint(根据你的需求进行配置)
    var config = Map<String, dynamic>.from({
      "cmpId": "你的CMP ID",          // 替换为你的CMP ID
      "gdprApplies": true,            // GDPR是否适用
      "debugMode": false,             // 是否启用调试模式
      "consentRequired": true,        // 是否需要用户同意
      "timeout": 5000,                // 请求超时时间(毫秒)
      // 其他配置参数...
    });

    try {
      await sourcepoint!.initialize(config);
      print("Sourcepoint initialized successfully");
    } catch (e) {
      print("Failed to initialize Sourcepoint: $e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Sourcepoint Unified CMP Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                onPressed: () async {
                  try {
                    var consentStatus = await sourcepoint!.getConsentStatus();
                    print("Current consent status: $consentStatus");
                  } catch (e) {
                    print("Failed to get consent status: $e");
                  }
                },
                child: Text('Get Consent Status'),
              ),
              ElevatedButton(
                onPressed: () async {
                  try {
                    await sourcepoint!.showCMP();
                  } catch (e) {
                    print("Failed to show CMP: $e");
                  }
                },
                child: Text('Show CMP'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

说明

  1. 初始化插件:在initState方法中,我们实例化了SourcepointUnifiedCmpAndroid对象,并调用initialize方法来进行初始化。

  2. 配置参数:你需要根据你的具体需求来配置Sourcepoint,比如CMP ID、GDPR是否适用、调试模式等。

  3. 获取用户同意状态:通过调用getConsentStatus方法可以获取当前的用户同意状态。

  4. 显示CMP界面:通过调用showCMP方法可以显示CMP(广告管理)界面,让用户进行隐私设置。

注意事项

  • 请确保你已经在Sourcepoint平台上创建了应用并获取了CMP ID。
  • 根据你的具体需求,你可能需要调整配置参数。
  • 在实际项目中,你应该处理更多的错误情况和边界情况。

希望这个示例能帮助你在Flutter项目中集成和使用sourcepoint_unified_cmp_android插件。

回到顶部