Flutter广告集成插件flutter_ironsource_x的使用
Flutter广告集成插件flutter_ironsource_x的使用
Banner
插屏广告/激励视频
优惠墙
在下一版本中,版本格式将如下所示:
xx.xx.xxxx
🠕 🠕 🠕
| | 发布修订版
| IronSource SDK 主要版本
Dart 主要版本
示例:
2.7.1
------------------------------
2 是 Dart 主要版本
7 是 IronSource 主要版本
1 是 发布修订版
IronSource 广告插件 Flutter 使用说明
初始化
void init() async {
var userId = await IronSource.getAdvertiserId();
await IronSource.validateIntegration();
await IronSource.setUserId(userId);
await IronSource.initialize(appKey: "appKey", listener: this,
gdprConsent: true, ccpaConsent: false);
rewardeVideoAvailable = await IronSource.isRewardedVideoAvailable();
offerwallAvailable = await IronSource.isOfferwallAvailable();
setState(() {});
}
默认情况下 gdprConsent
和 ccpaConsent
均为 true
。
插屏广告
IronSource.loadInterstitial();
void showInterstitial() async {
if (await IronSource.isInterstitialReady()) {
IronSource.showInterstitial();
} else {
print(
"插屏广告未准备好。请使用 'Ironsource.loadInterstial' 加载后再展示。",
);
}
}
@override
void onInterstitialAdClicked() {
print("onInterstitialAdClicked");
}
@override
void onInterstitialAdClosed() {
print("onInterstitialAdClosed");
}
@override
void onInterstitialAdLoadFailed(IronSourceError error) {
print("onInterstitialAdLoadFailed : ${error.toString()}");
}
@override
void onInterstitialAdOpened() {
print("onInterstitialAdOpened");
setState(() {
interstitialReady = false;
});
}
@override
void onInterstitialAdReady() {
print("onInterstitialAdReady");
setState(() {
interstitialReady = true;
});
}
@override
void onInterstitialAdShowFailed(IronSourceError error) {
print("onInterstitialAdShowFailed : ${error.toString()}");
setState(() {
interstitialReady = false;
});
}
@override
void onInterstitialAdShowSucceeded() {
print("nInterstitialAdShowSucceeded");
}
激励视频
void showRewardedVideo() async {
if (await IronSource.isRewardedVideoAvailable()) {
IronSource.showRewardedVideol();
} else {
print("激励视频不可用");
}
}
@override
void onRewardedVideoAdClicked(Placement placement) {
print("onRewardedVideoAdClicked");
}
@override
void onRewardedVideoAdClosed() {
print("onRewardedVideoAdClosed");
}
@override
void onRewardedVideoAdEnded() {
print("onRewardedVideoAdEnded");
}
@override
void onRewardedVideoAdOpened() {
print("onRewardedVideoAdOpened");
}
@override
void onRewardedVideoAdRewarded(Placement placement) {
print("onRewardedVideoAdRewarded: ${placement.placementName}");
}
@override
void onRewardedVideoAdShowFailed(IronSourceError error) {
print("onRewardedVideoAdShowFailed : ${error.toString()}");
}
@override
void onRewardedVideoAdStarted() {
print("onRewardedVideoAdStarted");
}
@override
void onRewardedVideoAvailabilityChanged(bool available) {
print("onRewardedVideoAvailabilityChanged : $available");
setState(() {
rewardeVideoAvailable = available;
});
}
Banner 广告
IronSourceBannerAd(keepAlive: true, listener: BannerAdListener());
Banner 尺寸类型
- BANNER
IronSourceBannerAd(keepAlive: true, listener: BannerAdListener(), size: BannerSize.BANNER);
- LARGE
IronSourceBannerAd(keepAlive: true, listener: BannerAdListener(), size: BannerSize.LARGE);
- LEADERBOARD
IronSourceBannerAd(keepAlive: true, listener: BannerAdListener(), size: BannerSize.LEADERBOARD);
- RECTANGLE
IronSourceBannerAd(keepAlive: true, listener: BannerAdListener(), size: BannerSize.RECTANGLE);
- SMART
IronSourceBannerAd(keepAlive: true, listener: BannerAdListener(), size: BannerSize.SMART);
- CUSTOM
IronSourceBannerAd(
keepAlive: true,
listener: BannerAdListener(),
size: BannerSize.BANNER,
size: BannerSize(
type: BannerSizeType.BANNER,
width: 400,
height: 50,
),
);
Banner 背景颜色
IronSourceBannerAd(
keepAlive: true,
listener: BannerAdListener(),
size: BannerSize.BANNER,
backgroundColor: Colors.amber, // 背景颜色
);
class BannerAdListener extends IronSourceBannerListener {
@override
void onBannerAdClicked() {
print("onBannerAdClicked");
}
@override
void onBannerAdLeftApplication() {
print("onBannerAdLeftApplication");
}
@override
void onBannerAdLoadFailed(Map<String, dynamic> error) {
print("onBannerAdLoadFailed");
}
@override
void onBannerAdLoaded() {
print("onBannerAdLoaded");
}
@override
void onBannerAdScreenDismissed() {
print("onBannerAdScreenDismisse");
}
@override
void onBannerAdScreenPresented() {
print("onBannerAdScreenPresented");
}
}
优惠墙
void showOfferwall() async {
if (await IronSource.isOfferwallAvailable()) {
IronSource.showOfferwall();
} else {
print("优惠墙不可用");
}
}
@override
void onGetOfferwallCreditsFailed(IronSourceError error) {
print("onGetOfferwallCreditsFailed : ${error.toString()}");
}
@override
void onOfferwallAdCredited(OfferwallCredit reward) {
print("onOfferwallAdCredited : $reward");
}
@override
void onOfferwallAvailable(bool available) {
print("onOfferwallAvailable : $available");
setState(() {
offerwallAvailable = available;
});
}
@override
void onOfferwallClosed() {
print("onOfferwallClosed");
}
@override
void onOfferwallOpened() {
print("onOfferwallOpened");
}
@override
void onOfferwallShowFailed(IronSourceError error) {
print("onOfferwallShowFailed ${error.toString()}");
}
更新 AndroidManifest.xml
权限
在 AndroidManifest.xml
文件中添加以下权限(位于 <manifest>
标签内但 <application>
标签外):
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
活动
在 AndroidManifest.xml
文件的 <application>
标签内添加以下活动:
<activity
android:name="com.ironsource.sdk.controller.ControllerActivity"
android:configChanges="orientation|screenSize"
android:hardwareAccelerated="true" />
<activity
android:name="com.ironsource.sdk.controller.InterstitialActivity"
android:configChanges="orientation|screenSize"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Translucent" />
<activity
android:name="com.ironsource.sdk.controller.OpenUrlActivity"
android:configChanges="orientation|screenSize"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Translucent" />
设置 android/app/build.gradle
在 android/app/build.gradle
中添加以下依赖:
dependencies {
implementation 'com.ironsource.sdk:mediationsdk:7.2.1'
...
}
将 compileSdkVersion
修改为至少 31
:
android {
compileSdkVersion 31
...
将 minSdkVersion
修改为至少 21
,targetSdkVersion
修改为至少 31
:
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 31
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
设置 android/build.gradle
将 ext.kotlin_version
修改为至少 1.6.10
:
buildscript {
ext.kotlin_version = '1.6.10'
...
添加 Google Play 服务
在顶部添加 xmlns:tools="http://schemas.android.com/tools"
:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.metamorfosis_labs.flutter_ironsource_x_example">
在 <application>
标签内添加 tools:replace="android:label"
:
<application
tools:replace="android:label"
android:name="io.flutter.app.FlutterApplication"
android:label="flutter_ironsource_x_example"
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"
android:icon="@mipmap/ic_launcher">
在 <application>
标签内添加以下内容:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
<meta-data android:name="com.google.android.gms.version"
android:value="[@integer](/user/integer)/google_play_services_version" />
更多关于Flutter广告集成插件flutter_ironsource_x的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter广告集成插件flutter_ironsource_x的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter应用中集成和使用flutter_ironsource_x
插件的示例代码。这个插件允许你集成IronSource广告网络到你的Flutter应用中。
首先,确保你已经在你的pubspec.yaml
文件中添加了flutter_ironsource_x
依赖:
dependencies:
flutter:
sdk: flutter
flutter_ironsource_x: ^最新版本号 # 请替换为实际的最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,你需要在你的Flutter应用中初始化IronSource SDK,并展示广告。以下是一个简单的示例:
1. 初始化IronSource SDK
在你的应用的主入口文件(通常是main.dart
)中初始化IronSource SDK。确保在展示任何广告之前完成初始化。
import 'package:flutter/material.dart';
import 'package:flutter_ironsource_x/flutter_ironsource_x.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
IronSource.initialize(
placementId: '你的Interstitial广告位ID', // 替换为你的Interstitial广告位ID
appId: '你的IronSource App ID', // 替换为你的IronSource App ID
adUnitIdMap: {
'Interstitial': '你的Interstitial广告位ID', // 可以添加多个广告位ID
'RewardedVideo': '你的RewardedVideo广告位ID',
// ... 可以添加更多广告类型
},
listener: (IronSourceEvent event) {
print('IronSource Event: ${event.message}');
},
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('IronSource Flutter Integration'),
),
body: Center(
child: MyHomePage(),
),
),
);
}
}
2. 展示Interstitial广告
在你的主页(例如MyHomePage.dart
)中,添加一个按钮来展示Interstitial广告。
import 'package:flutter/material.dart';
import 'package:flutter_ironsource_x/flutter_ironsource_x.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () async {
bool isAvailable = await IronSource.isInterstitialAvailable();
if (isAvailable) {
IronSource.showInterstitial();
} else {
print('Interstitial ad is not available.');
}
},
child: Text('Show Interstitial Ad'),
),
// 你可以在这里添加更多按钮来展示其他类型的广告,例如RewardedVideo
],
);
}
}
3. 处理奖励视频广告(可选)
如果你还想集成RewardedVideo广告,你可以按照类似的方式处理。首先,在初始化时添加RewardedVideo的广告位ID,然后在需要的地方展示广告并处理奖励回调。
// 在IronSource.initialize中添加RewardedVideo的广告位ID
adUnitIdMap: {
'Interstitial': '你的Interstitial广告位ID',
'RewardedVideo': '你的RewardedVideo广告位ID',
},
// 在你的UI中添加一个按钮来展示RewardedVideo广告
ElevatedButton(
onPressed: () async {
bool isAvailable = await IronSource.isRewardedVideoAvailable();
if (isAvailable) {
IronSource.showRewardedVideo(
listener: (IronSourceEvent event) {
if (event.type == IronSourceEventType.REWARDED_VIDEO_REWARD) {
// 处理奖励回调
print('User received reward: ${event.rewardAmount}');
}
},
);
} else {
print('Rewarded Video ad is not available.');
}
},
child: Text('Show Rewarded Video Ad'),
),
确保你已经按照IronSource的文档要求配置了你的项目,并获取了必要的广告位ID和App ID。
以上代码提供了一个基本的框架,你可以根据需要进行扩展和修改。注意,实际使用中你可能需要处理更多的错误情况和用户交互逻辑。