Flutter广告展示插件adpopcornssp的使用

Flutter广告展示插件adpopcornssp的使用

当前adpopcornssp SDK版本

  • Android: 3.5.0
  • iOS: 2.7.0

安装插件

pubspec.yaml 文件中添加依赖:

dependencies:
  adpopcornssp: ^1.0.5

集成指南

要查看更多信息,请参阅 AdPopcornSSP集成指南

示例代码

以下是一个完整的示例代码,展示了如何在Flutter应用中使用adpopcornssp插件来展示广告。

main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io' show Platform;

import 'package:flutter/services.dart';
import 'package:adpopcornssp/adpopcornssp.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static const MethodChannel androidBannerChannel = const MethodChannel('adpopcornssp/BANNER_320x50');
  static const MethodChannel iosBannerChannel = const MethodChannel('adpopcornssp/iOS_BANNER_320x50');

  static const MethodChannel androidNativeChannel = const MethodChannel('adpopcornssp/NATIVE_TEMPLATE');
  static const MethodChannel iosNativeChannel = const MethodChannel('adpopcornssp/iOS_NATIVE_TEMPLATE');

  [@override](/user/override)
  void initState() {
    super.initState();
    if (Platform.isAndroid) {
      AdPopcornSSP.init('663451319');
      // 加载全屏广告
      AdPopcornSSP.loadInterstitial('663451319', 'INTERSTITIAL');
      AdPopcornSSP.interstitialAdLoadSuccessListener = (placementId) {
        // AdPopcornSSP.showInterstitial('663451319', placementId);
      };

      // 加载全屏视频广告
      AdPopcornSSP.loadInterstitialVideo('663451319', 'VIDEO');
      AdPopcornSSP.interstitialVideoAdLoadSuccessListener = (placementId) {
        // AdPopcornSSP.showInterstitialVideo('663451319', placementId);
      };

      // 加载奖励视频广告
      AdPopcornSSP.loadRewardVideo('663451319', 'REWARD_VIDEO');
      AdPopcornSSP.rewardVideoAdLoadSuccessListener = (placementId) {
        // AdPopcornSSP.showRewardVideo('663451319', placementId);
      };

      // 连接广告事件通道
      androidBannerChannel.setMethodCallHandler(_eventHandleMethod);
      androidNativeChannel.setMethodCallHandler(_eventHandleMethod);
    } else if (Platform.isIOS) {
      AdPopcornSSP.init('397261446');

      // 加载全屏广告
      AdPopcornSSP.loadInterstitial('397261446', 'iOS_INTERSTITIAL');
      AdPopcornSSP.interstitialAdLoadSuccessListener = (placementId) {
        AdPopcornSSP.showInterstitial('397261446', placementId);
      };

      // 加载全屏视频广告
      AdPopcornSSP.loadInterstitialVideo('397261446', 'iOS_VIDEO');
      AdPopcornSSP.interstitialVideoAdLoadSuccessListener = (placementId) {
        // AdPopcornSSP.showInterstitialVideo('397261446', placementId);
      };

      // 加载奖励视频广告
      AdPopcornSSP.loadRewardVideo('397261446', 'iOS_REWARD_VIDEO');
      AdPopcornSSP.rewardVideoAdLoadSuccessListener = (placementId) {
        // AdPopcornSSP.showRewardVideo('397261446', placementId);
      };

      // 连接广告事件通道
      iosBannerChannel.setMethodCallHandler(_eventHandleMethod);
      iosNativeChannel.setMethodCallHandler(_eventHandleMethod);
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('插件示例应用'),
        ),
        body: _body(),
      ),
    );
  }

  Widget _body() {
    return ListView(
      children: _listItem(),
    );
  }

  List<Widget> _listItem() {
    List<Widget> widgets = [];
    widgets.add(_setBannerView());
    widgets.add(_setNativeView());
    return widgets;
  }

  Widget _setBannerView() {
    const String viewType = 'AdPopcornSSPBannerView';
    if (Platform.isAndroid) {
      final Map<String, dynamic> creationParams = {'appKey': '663451319', 'placementId': 'BANNER_320x50', 'bannerSize': '320x50'};
      return Container(
        width: double.maxFinite,
        height: 50,
        child: AndroidView(
          viewType: viewType,
          layoutDirection: TextDirection.ltr,
          creationParams: creationParams,
          creationParamsCodec: const StandardMessageCodec(),
        ),
      );
    } else if (Platform.isIOS) {
      final Map<String, dynamic> creationParams = {'appKey': '397261446', 'placementId': 'iOS_BANNER_320x50', 'bannerSize': '320x50'};
      return Container(
        width: double.maxFinite,
        height: 50,
        child: UiKitView(
          viewType: viewType,
          layoutDirection: TextDirection.ltr,
          creationParams: creationParams,
          creationParamsCodec: const StandardMessageCodec(),
        ),
      );
    } else {
      return Container(
        width: double.maxFinite,
        height: 1,
      );
    }
  }

  Widget _setNativeView() {
    const String viewType = 'AdPopcornSSPNativeView';
    if (Platform.isAndroid) {
      final Map<String, dynamic> creationParams = {'appKey': '663451319', 'placementId': 'NATIVE_TEMPLATE'};
      return Container(
        width: double.maxFinite,
        height: 280,
        child: AndroidView(
          viewType: viewType,
          layoutDirection: TextDirection.ltr,
          creationParams: creationParams,
          creationParamsCodec: const StandardMessageCodec(),
        ),
      );
    } else if (Platform.isIOS) {
      final Map<String, dynamic> creationParams = {'appKey': '397261446', 'placementId': 'iOS_NATIVE_TEMPLATE'};
      return Container(
        width: double.maxFinite,
        height: 280,
        child: UiKitView(
          viewType: viewType,
          layoutDirection: TextDirection.ltr,
          creationParams: creationParams,
          creationParamsCodec: const StandardMessageCodec(),
        ),
      );
    } else {
      return Container(
        width: double.maxFinite,
        height: 1,
      );
    }
  }

  static Future<dynamic> _eventHandleMethod(MethodCall call) async {
    print('_eventHandleMethod: ${call.method}, ${call.arguments}');
    final Map<dynamic, dynamic> arguments = call.arguments;
    final String method = call.method;

    final String placementId = arguments['placementId'];
    if (method == 'APSSPBannerViewLoadSuccess') {
      print('main.dart APSSPBannerViewLoadSuccess');
    } else if (method == 'APSSPBannerViewLoadFail') {
      final int errorCode = arguments['errorCode'];
      print('main.dart APSSPBannerViewLoadFail');
    } else if (method == 'APSSPBannerViewClicked') {
      print('main.dart APSSPBannerViewClicked');
    } else if (method == 'APSSPNativeAdLoadSuccess') {
      print('main.dart APSSPNativeAdLoadSuccess');
    } else if (method == 'APSSPNativeAdLoadFail') {
      final int errorCode = arguments['errorCode'];
      print('main.dart APSSPNativeAdLoadFail');
    } else if (method == 'APSSPNativeAdImpression') {
      print('main.dart APSSPNativeAdImpression');
    } else if (method == 'APSSPNativeAdClicked') {
      print('main.dart APSSPNativeAdClicked');
    }
    return Future<dynamic>.value(null);
  }
}

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

1 回复

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


adpopcornssp 是一个用于在 Flutter 应用中展示广告的插件,特别是通过 AdPopcorn SSP(Supply Side Platform)平台提供的广告。以下是使用 adpopcornssp 插件的基本步骤和示例代码。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 adpopcornssp 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  adpopcornssp: ^1.0.0  # 请检查最新版本

然后运行 flutter pub get 来获取依赖。

2. 初始化插件

在你的 Flutter 应用中初始化 adpopcornssp 插件。通常你可以在 main.dart 中进行初始化。

import 'package:adpopcornssp/adpopcornssp.dart';

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

  // 初始化 AdPopcorn SSP
  AdPopcornSSP.initialize(
    appKey: 'YOUR_APP_KEY',  // 替换为你的 App Key
    isTest: true,            // 测试模式,发布时设置为 false
  );
}

3. 展示横幅广告

你可以使用 BannerAdWidget 来展示横幅广告。

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

class BannerAdExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Banner Ad Example'),
      ),
      body: Column(
        children: [
          Expanded(
            child: Center(
              child: Text('Content goes here'),
            ),
          ),
          BannerAdWidget(
            adUnitId: 'YOUR_BANNER_AD_UNIT_ID',  // 替换为你的横幅广告单元 ID
            adSize: BannerAdSize.BANNER,
          ),
        ],
      ),
    );
  }
}

4. 展示插屏广告

你可以使用 InterstitialAd 来展示插屏广告。

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

class InterstitialAdExample extends StatelessWidget {
  Future<void> _showInterstitialAd() async {
    InterstitialAd interstitialAd = InterstitialAd(
      adUnitId: 'YOUR_INTERSTITIAL_AD_UNIT_ID',  // 替换为你的插屏广告单元 ID
    );
    await interstitialAd.load();
    await interstitialAd.show();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Interstitial Ad Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _showInterstitialAd,
          child: Text('Show Interstitial Ad'),
        ),
      ),
    );
  }
}

5. 展示奖励广告

你可以使用 RewardedAd 来展示奖励广告。

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

class RewardedAdExample extends StatelessWidget {
  Future<void> _showRewardedAd() async {
    RewardedAd rewardedAd = RewardedAd(
      adUnitId: 'YOUR_REWARDED_AD_UNIT_ID',  // 替换为你的奖励广告单元 ID
    );
    rewardedAd.onRewarded = (reward) {
      print('Reward: $reward');
    };
    await rewardedAd.load();
    await rewardedAd.show();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
 title: Text('Rewarded Ad Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _showRewardedAd,
          child: Text('Show Rewarded Ad'),
        ),
      ),
    );
  }
}

6. 处理广告事件

你可以监听广告的生命周期事件,例如广告加载成功、展示、点击、关闭等。

BannerAdWidget(
  adUnitId: 'YOUR_BANNER_AD_UNIT_ID',
  adSize: BannerAdSize.BANNER,
  onAdLoaded: () {
    print('Banner Ad Loaded');
  },
  onAdFailedToLoad: (error) {
    print('Banner Ad Failed to Load: $error');
  },
  onAdClicked: () {
    print('Banner Ad Clicked');
  },
  onAdClosed: () {
    print('Banner Ad Closed');
  },
),

7. 发布应用

在发布应用之前,请确保将 AdPopcornSSP.initialize 中的 isTest 参数设置为 false,以便展示真实的广告。

AdPopcornSSP.initialize(
  appKey: 'YOUR_APP_KEY',
  isTest: false,  // 发布时设置为 false
);
回到顶部