google_mobile_ads
패키지를 추가합니다.flutter pub add google_mobile_ads
AndroidManifest.xml
에 다음과 같은 권한과 메타데이터를 추가해야 합니다.<!-- AndroidManifest.xml -->
<manifest>
<application>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
</application>
</manifest>
Info.plist
파일에 다음과 같은 키를 추가합니다.<key>GADApplicationIdentifier</key>
<string>ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy</string>
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
class RewardedInterstitialAdExample extends StatefulWidget {
const RewardedInterstitialAdExample({super.key});
@override
RewardedInterstitialAdExampleState createState() =>
RewardedInterstitialAdExampleState();
}
class RewardedInterstitialAdExampleState
extends State<RewardedInterstitialAdExample> {
RewardedInterstitialAd? _rewardedInterstitialAd;
bool _isAdLoaded = false;
void _loadAd() {
RewardedInterstitialAd.load(
adUnitId: 'ca-app-pub-3940256099942544/5354046379', // 테스트 ID
request: const AdRequest(),
rewardedInterstitialAdLoadCallback: RewardedInterstitialAdLoadCallback(
onAdLoaded: (ad) {
log('onAdLoaded: 광고 로드 완료');
setState(() {
_rewardedInterstitialAd = ad;
_isAdLoaded = true;
});
},
onAdFailedToLoad: (error) {
log('onAdFailedToLoad: 광고 로드 실패: $error');
},
),
);
}
void _showAd() {
if (_isAdLoaded && _rewardedInterstitialAd != null) {
_rewardedInterstitialAd!.fullScreenContentCallback =
FullScreenContentCallback(
onAdDismissedFullScreenContent: (ad) {
log('onAdDismissedFullScreenContent: 광고가 닫혔습니다.');
ad.dispose();
},
onAdFailedToShowFullScreenContent: (ad, error) {
log('onAdFailedToShowFullScreenContent: 광고 표시 실패: $error');
},
);
_rewardedInterstitialAd!.show(onUserEarnedReward: (ad, reward) {
log('onUserEarnedReward: 사용자가 보상을 획득했습니다: ${reward.amount} ${reward.type}');
// 보상 로직 처리
});
_rewardedInterstitialAd = null;
_isAdLoaded = false;
} else {
log('onAdFailedToShowFullScreenContent: 광고가 아직 준비되지 않았습니다.');
}
}
@override
void initState() {
super.initState();
_loadAd();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('보상형 전면 광고 예제')),
body: Center(
child: ElevatedButton(
onPressed: _showAd,
child: const Text('광고 보기'),
),
),
);
}
}
_isAdLoaded
상태를 이용해 버튼 활성화를 제어할 수 있습니다. 또한 광고 실패나 닫힘 이벤트를 리슨해서 앱 흐름을 자연스럽게 유지해야 합니다.
onAdLoaded
: 광고 페이지 로드 시 호출됩니다.onAdFailedToLoad
: 광고 로드 실패 시 호출됩니다.onAdDismissedFullScreenContent
: 광고를 시청 완료 후 다시 로드할 때 호출됩니다.onAdFailedToShowFullScreenContent
: 광고 표시 실패 시 호출됩니다.onUserEarnedReward
: 사용자가 보상을 획득했을 때 호출됩니다.onAdFailedToLoad: 광고 로드 실패: LoadAdError(code: 0, domain: com.google.android.gms.ads, message: Error while connecting to ad server: Unable to resolve host "googleads.g.doubleclick.net": No address associated with hostname, responseInfo: ResponseInfo(responseId: null, mediationAdapterClassName: , adapterResponses: [], loadedAdapterResponseInfo: null), responseExtras: {})
google_mobile_ads
패키지를 통해 쉽게 적용할 수 있으며, 적절한 이벤트 리슨과 상태 관리를 통해 광고 흐름을 부드럽게 구성할 수 있습니다.
이번 글에서는 AdMob 설정부터 Flutter 코드 작성, 이벤트 리슨까지 전반적인 흐름을 살펴보았습니다. 직접 테스트 광고로 연습해보신 후, 실제 광고 ID로 교체해 사용하시면 되겠습니다.