Flutter Mobile Ads

本文最后更新于:2025年5月23日 上午

使用插件:google_mobile_ads
文档地址:Google AdMob

这个插件的作用是在 Flutter 应用中集成 Google Mobile Ads SDK,通过 Google 的广告平台在应用中展示广告,从而实现应用的变现。没错就是你玩游戏的时候看到的那些魔性小广告。我们集成这个插件的目的就是为了在应用中展示广告,用户获取应用内积分用于购买虚拟物品,我们获取广告收益。

1. 注册Google AdMob账号

google家的嘛,你得先有个google账号才能去注册;
注册地址:Google AdMob

填写账号信息,收款银行卡信息等等这些就按照要求填吧

注意事项:账号注册的地区写了就不能改了,这个地区要和收款银行卡一致,据说后面google还会往这个地区的一个地址里面寄一个包裹邮件,包含pin码,必须要校验这个码,才能收钱(我还没走到这一步项目就暂停了,非常可惜);

下一步还需要验证这个地区的一个手机验证码,我第一次注册写的是美国,尴尬了,又注册了第二个是中国。

2. 创建应用,分别创建android和ios应用

没有项目的概念,直接添加应用,android和ios各一个,每个应用里面可以设置广告单元,不同的广告单元对应不同的虚拟价值。
广告类型有四种:

  • 横幅广告
    横幅广告是在设备屏幕的顶部或底部展示的矩形广告。它们可以是静态的或动态的,并且可以包含文本、图像或视频。横幅广告会停留在界面上,并且可在一段时间后自动刷新。占用一部分屏幕,很让人讨厌的那种,哈哈。
  • 插屏广告
    插屏广告是在应用的自然停顿点展示的全屏广告。它们通常在用户完成一个任务或操作后出现,例如在游戏关卡结束时。这个广告类型是最常见的,就是被动观看有点烦人。
  • 激励广告
    激励广告是全屏广告,用户观看广告后可以获得奖励,例如游戏内货币或额外的生命。用户必须主动选择观看这些广告,并且在观看完毕后才能获得奖励。这个广告类型是用户为了获取奖励,自己主动点击的,所以接受度还行,我用的也是这种。
  • 原生广告
    原生广告是与应用内容无缝集成的广告。它们可以根据应用的外观和感觉进行自定义,以提供更好的用户体验,并且可以在应用的不同部分展示。这个广告类型是最难做的,要求设计师和开发者配合,设计出一个和应用内容无缝集成的广告。

初始化插件的代码如下:

1
2
3
4
5
6
import 'package:google_mobile_ads/google_mobile_ads.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(MyApp());
}

2.1 创建并配置Android项目:

填写基本信息略;
配置:
AdMob 应用 ID 必须包含在 AndroidManifest.xml 中,否则会导致应用在启动时崩溃。
修改这个文件:android/app/src/main/AndroidManifest.xml

1
2
3
4
5
6
7
8
<manifest>
<application>
<!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
<application>
<manifest>

2.2 创建并配置ios项目:

填写基本信息略;
配置:
AdMob 应用 ID 必须包含在 Info.plist 中。
修改这个文件:ios/Runner/Info.plist

1
2
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-################~##########</string>

注意事项:ios配置的时候我把GADApplicationIdentifier写到上一个facebook配置的array里面去了,太傻逼了,app直接崩溃了,希望你别学我。

3. 播放广告示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import 'package:get/get_utils/src/platform/platform.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

class GoogleAds {
/// 这是广告单元信息--测试阶段是写的假的-todo
static final adUnitId = GetPlatform.isAndroid
? 'ca-app-pub-3940256099942544/5224354917'
: 'ca-app-pub-3940256099942544/1712485313';
/// 广告
static RewardedAd? _rewardedAd;
/// 加载广告
static googleAdsLoad() async {
RewardedAd.load(
adUnitId: adUnitId,
request: const AdRequest(),
rewardedAdLoadCallback: RewardedAdLoadCallback(
// Called when an ad is successfully received.
onAdLoaded: (ad) {
ad.fullScreenContentCallback = FullScreenContentCallback(
// Called when the ad showed the full screen content.
onAdShowedFullScreenContent: (ad) {},
// Called when an impression occurs on the ad.
onAdImpression: (ad) {},
// Called when the ad failed to show full screen content.
onAdFailedToShowFullScreenContent: (ad, err) {
// Dispose the ad here to free resources.
ad.dispose();
},
// Called when the ad dismissed full screen content.
onAdDismissedFullScreenContent: (ad) {
// Dispose the ad here to free resources.
ad.dispose();
},
// Called when a click is recorded for an ad.
onAdClicked: (ad) {});

print('$ad loaded.');
// Keep a reference to the ad so you can show it later.
_rewardedAd = ad;
},
// Called when an ad request failed.
onAdFailedToLoad: (LoadAdError error) {
print('RewardedAd failed to load: $error');
},
),
);
}
/// 展示广告
static showAds(){
_rewardedAd?.show(onUserEarnedReward: (AdWithoutView ad, RewardItem rewardItem) {
// Reward the user for watching an ad.
});
}
/// 销毁广告
static disposeAds() {
_rewardedAd?.dispose();
}
}

Flutter Mobile Ads
http://bestkele.com/2023/11/08/flutter/flutter-mobile-ads/
作者
kele
发布于
2023年11月8日
许可协议