feat(m7a): integrate admob app id with test ad units
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
|
||||
/// AdMob integration.
|
||||
///
|
||||
/// The **application id** lives in `AndroidManifest.xml`
|
||||
/// (`ca-app-pub-3909261635234286~8769927442`).
|
||||
///
|
||||
/// Until real ad units exist, Google's official **test units** are used on
|
||||
/// purpose: showing live ads during development produces invalid traffic and can
|
||||
/// get the AdMob account suspended.
|
||||
class AdsService {
|
||||
/// Official Google test unit for rewarded video.
|
||||
static const String rewardedUnitId =
|
||||
'ca-app-pub-3940256099942544/5224354917';
|
||||
|
||||
/// Official Google test unit for interstitials.
|
||||
static const String interstitialUnitId =
|
||||
'ca-app-pub-3940256099942544/1033173712';
|
||||
|
||||
/// Never show an interstitial during the first matches (retention).
|
||||
static const int skipFirstMatches = 3;
|
||||
|
||||
/// After that, at most every second match.
|
||||
static const int everyNthMatch = 2;
|
||||
|
||||
RewardedAd? _rewarded;
|
||||
InterstitialAd? _interstitial;
|
||||
bool _initialized = false;
|
||||
int _matchesFinished = 0;
|
||||
|
||||
bool get isInitialized => _initialized;
|
||||
bool get isRewardedReady => _rewarded != null;
|
||||
int get matchesFinished => _matchesFinished;
|
||||
|
||||
/// Initialises the SDK once and preloads both formats.
|
||||
Future<void> initialize() async {
|
||||
if (_initialized) return;
|
||||
await MobileAds.instance.initialize();
|
||||
_initialized = true;
|
||||
debugPrint('ads: SDK initialised');
|
||||
_loadRewarded();
|
||||
_loadInterstitial();
|
||||
}
|
||||
|
||||
void _loadRewarded() {
|
||||
RewardedAd.load(
|
||||
adUnitId: rewardedUnitId,
|
||||
request: const AdRequest(),
|
||||
rewardedAdLoadCallback: RewardedAdLoadCallback(
|
||||
onAdLoaded: (ad) {
|
||||
_rewarded = ad;
|
||||
debugPrint('ads: rewarded ready');
|
||||
},
|
||||
onAdFailedToLoad: (error) {
|
||||
_rewarded = null;
|
||||
debugPrint('ads: rewarded failed (${error.code}) ${error.message}');
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _loadInterstitial() {
|
||||
InterstitialAd.load(
|
||||
adUnitId: interstitialUnitId,
|
||||
request: const AdRequest(),
|
||||
adLoadCallback: InterstitialAdLoadCallback(
|
||||
onAdLoaded: (ad) {
|
||||
_interstitial = ad;
|
||||
debugPrint('ads: interstitial ready');
|
||||
},
|
||||
onAdFailedToLoad: (error) {
|
||||
_interstitial = null;
|
||||
debugPrint('ads: interstitial failed (${error.code}) ${error.message}');
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Shows a rewarded video and returns the granted amount, or null if none was
|
||||
/// available (the caller must not grant a reward in that case).
|
||||
Future<int?> showRewarded() async {
|
||||
final ad = _rewarded;
|
||||
if (ad == null) {
|
||||
_loadRewarded();
|
||||
return null;
|
||||
}
|
||||
_rewarded = null;
|
||||
int? earned;
|
||||
ad.fullScreenContentCallback = FullScreenContentCallback(
|
||||
onAdDismissedFullScreenContent: (ad) {
|
||||
ad.dispose();
|
||||
_loadRewarded();
|
||||
},
|
||||
onAdFailedToShowFullScreenContent: (ad, error) {
|
||||
debugPrint('ads: rewarded show failed ${error.code}');
|
||||
ad.dispose();
|
||||
_loadRewarded();
|
||||
},
|
||||
);
|
||||
await ad.show(onUserEarnedReward: (ad, reward) {
|
||||
earned = reward.amount.toInt();
|
||||
debugPrint('ads: reward ${reward.amount} ${reward.type}');
|
||||
});
|
||||
return earned;
|
||||
}
|
||||
|
||||
/// Called when a match ends; applies the frequency caps from the GDD.
|
||||
void onMatchFinished() {
|
||||
_matchesFinished++;
|
||||
if (_matchesFinished <= skipFirstMatches) return;
|
||||
if (_matchesFinished % everyNthMatch != 0) return;
|
||||
final ad = _interstitial;
|
||||
if (ad == null) {
|
||||
_loadInterstitial();
|
||||
return;
|
||||
}
|
||||
_interstitial = null;
|
||||
ad.fullScreenContentCallback = FullScreenContentCallback(
|
||||
onAdDismissedFullScreenContent: (ad) {
|
||||
ad.dispose();
|
||||
_loadInterstitial();
|
||||
},
|
||||
onAdFailedToShowFullScreenContent: (ad, error) {
|
||||
debugPrint('ads: interstitial show failed ${error.code}');
|
||||
ad.dispose();
|
||||
_loadInterstitial();
|
||||
},
|
||||
);
|
||||
ad.show();
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_rewarded?.dispose();
|
||||
_interstitial?.dispose();
|
||||
_rewarded = null;
|
||||
_interstitial = null;
|
||||
}
|
||||
}
|
||||
@@ -96,12 +96,14 @@ class Controls extends StatelessWidget {
|
||||
required this.onOnline,
|
||||
required this.onReplay,
|
||||
required this.onFlush,
|
||||
required this.onAd,
|
||||
});
|
||||
|
||||
final MatchSession session;
|
||||
final VoidCallback onOnline;
|
||||
final VoidCallback onReplay;
|
||||
final VoidCallback onFlush;
|
||||
final VoidCallback onAd;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -115,6 +117,7 @@ class Controls extends StatelessWidget {
|
||||
label: session.queued > 0 ? 'Senden (${session.queued})' : 'Senden',
|
||||
onTap: onFlush,
|
||||
),
|
||||
_Action(icon: Icons.ondemand_video, label: 'Ad', onTap: onAd),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user