feat(m7a): integrate admob app id with test ad units

This commit is contained in:
2026-10-25 00:21:32 +02:00
parent 2058dbdafa
commit f91eeba525
11 changed files with 235 additions and 4 deletions
+6 -1
View File
@@ -1,8 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Needed to reach the replay referee. -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:label="iron_arc"
android:label="Iron Arc"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
@@ -27,6 +28,10 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- AdMob application id (public). Ad unit ids live in lib/src/ads/ads_service.dart. -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3909261635234286~8769927442" />
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
+12
View File
@@ -2,6 +2,7 @@ import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'src/ads/ads_service.dart';
import 'src/game/iron_arc_game.dart';
import 'src/native/native_match.dart';
import 'src/net/api_client.dart';
@@ -39,6 +40,7 @@ class _MatchScreenState extends State<MatchScreen> {
int _seed = 42;
late final ApiClient _api = ApiClient(baseUrl: _baseUrl);
late final OfflineQueue _queue = OfflineQueue();
late final AdsService _ads = AdsService();
late final MatchSession _session =
MatchSession(client: _api, queue: _queue, deviceId: 'pending');
late IronArcGame _game = _buildGame();
@@ -47,10 +49,12 @@ class _MatchScreenState extends State<MatchScreen> {
void initState() {
super.initState();
_initSession();
_ads.initialize();
}
@override
void dispose() {
_ads.dispose();
_api.close();
_session.dispose();
super.dispose();
@@ -84,6 +88,7 @@ class _MatchScreenState extends State<MatchScreen> {
} else if (game.hud.value.isFinished) {
_session.enqueueReplay(game.replayJson());
}
if (game.hud.value.isFinished) _ads.onMatchFinished();
};
return game;
}
@@ -112,6 +117,12 @@ class _MatchScreenState extends State<MatchScreen> {
_toast('Replay läuft');
}
Future<void> _showRewardedAd() async {
final reward = await _ads.showRewarded();
if (!mounted) return;
_toast(reward == null ? 'Keine Anzeige verfügbar' : 'Belohnung: +$reward');
}
Future<void> _flushQueue() async {
final validated = await _session.flushQueue();
if (mounted) _toast('$validated Replay(s) validiert');
@@ -193,6 +204,7 @@ class _MatchScreenState extends State<MatchScreen> {
onOnline: _startOnline,
onReplay: _playReplay,
onFlush: _flushQueue,
onAd: _showRewardedAd,
),
),
),
+139
View File
@@ -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;
}
}
+3
View File
@@ -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),
],
);
}
+41 -1
View File
@@ -112,6 +112,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
google_mobile_ads:
dependency: "direct main"
description:
name: google_mobile_ads
sha256: "439e018d73b47565d1cf21410e893a8f46d5fb95110c41c266a6fb78449d7106"
url: "https://pub.dev"
source: hosted
version: "9.2.0"
http:
dependency: "direct main"
description:
@@ -405,6 +413,38 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.2"
webview_flutter:
dependency: transitive
description:
name: webview_flutter
sha256: d53e1ccf5516f25017e3c9d44c39034db352d20fa34fe200674270242c2c5111
url: "https://pub.dev"
source: hosted
version: "4.14.1"
webview_flutter_android:
dependency: transitive
description:
name: webview_flutter_android
sha256: "4de8b3d1ff4ebe1bdb42e68a5e4f809194a3cb0117a8f495f590004f00da3964"
url: "https://pub.dev"
source: hosted
version: "4.14.1"
webview_flutter_platform_interface:
dependency: transitive
description:
name: webview_flutter_platform_interface
sha256: "1221c1b12f5278791042f2ec2841743784cf25c5a644e23d6680e5d718824f04"
url: "https://pub.dev"
source: hosted
version: "2.15.1"
webview_flutter_wkwebview:
dependency: transitive
description:
name: webview_flutter_wkwebview
sha256: acac45e0a8d7beb1d0805521f44842f406c46eb5a69410f53d935e3c62138afc
url: "https://pub.dev"
source: hosted
version: "3.27.2"
xdg_directories:
dependency: transitive
description:
@@ -415,4 +455,4 @@ packages:
version: "1.1.0"
sdks:
dart: ">=3.13.4 <4.0.0"
flutter: ">=3.44.0"
flutter: ">=3.47.0"
+1
View File
@@ -38,6 +38,7 @@ dependencies:
ffi: ^2.2.0
http: ^1.6.0
shared_preferences: ^2.5.6
google_mobile_ads: ^9.2.0
dev_dependencies:
flutter_test: