Skip to content

Commit 251e8da

Browse files
committed
feat(ads): implement AdMobAdProvider for Google AdMob integration
- Add AdMobAdProvider class implementing AdProvider interface - Include initialization of Google Mobile Ads SDK - Implement native ad loading for AdMob with platform-specific unit IDs - Add logging for ad events and errors - Support Android, iOS, and Web platforms
1 parent 520bc46 commit 251e8da

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

lib/ads/admob_ad_provider.dart

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter/foundation.dart';
4+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_provider.dart';
5+
import 'package:google_mobile_ads/google_mobile_ads.dart';
6+
import 'package:logging/logging.dart';
7+
8+
/// {@template admob_ad_provider}
9+
/// A concrete implementation of [AdProvider] for Google AdMob.
10+
///
11+
/// This class handles the initialization of the Google Mobile Ads SDK
12+
/// and the loading of native ads specifically for AdMob.
13+
/// {@endtemplate}
14+
class AdMobAdProvider implements AdProvider {
15+
/// {@macro admob_ad_provider}
16+
AdMobAdProvider({Logger? logger}) : _logger = logger ?? Logger('AdMobAdProvider');
17+
18+
final Logger _logger;
19+
20+
/// The AdMob Native Ad Unit ID for Android.
21+
///
22+
/// This should be replaced with your production Ad Unit ID.
23+
/// For testing, use `ca-app-pub-3940256099942544/2247696110`.
24+
static const String _androidNativeAdUnitId =
25+
'ca-app-pub-3940256099942544/2247696110';
26+
27+
/// The AdMob Native Ad Unit ID for iOS.
28+
///
29+
/// This should be replaced with your production Ad Unit ID.
30+
/// For testing, use `ca-app-pub-3940256099942544/3986624511`.
31+
static const String _iosNativeAdUnitId =
32+
'ca-app-pub-3940256099942544/3986624511';
33+
34+
/// The AdMob Native Ad Unit ID for Web.
35+
///
36+
/// AdMob does not officially support native ads on web. This is a placeholder.
37+
/// For testing, use `ca-app-pub-3940256099942544/2247696110` (Android test ID).
38+
static const String _webNativeAdUnitId =
39+
'ca-app-pub-3940256099942544/2247696110';
40+
41+
/// Returns the appropriate native ad unit ID based on the platform.
42+
String get _nativeAdUnitId {
43+
if (kIsWeb) {
44+
return _webNativeAdUnitId;
45+
} else if (defaultTargetPlatform == TargetPlatform.android) {
46+
return _androidNativeAdUnitId;
47+
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
48+
return _iosNativeAdUnitId;
49+
}
50+
return ''; // Fallback for unsupported platforms
51+
}
52+
53+
@override
54+
Future<void> initialize() async {
55+
_logger.info('Initializing Google Mobile Ads SDK...');
56+
try {
57+
await MobileAds.instance.initialize();
58+
_logger.info('Google Mobile Ads SDK initialized successfully.');
59+
} catch (e) {
60+
_logger.severe('Failed to initialize Google Mobile Ads SDK: $e');
61+
// TODO(fulleni): Depending on requirements, you might want to rethrow or handle this more gracefully.
62+
// For now, we log and continue, as ad loading might still work in some cases.
63+
}
64+
}
65+
66+
@override
67+
Future<NativeAd?> loadNativeAd() async {
68+
if (_nativeAdUnitId.isEmpty) {
69+
_logger.warning('No native ad unit ID configured for this platform.');
70+
return null;
71+
}
72+
73+
_logger.info('Attempting to load native ad from unit ID: $_nativeAdUnitId');
74+
75+
final completer = Completer<NativeAd?>();
76+
77+
final ad = NativeAd(
78+
adUnitId: _nativeAdUnitId,
79+
factoryId: 'listTile', // This ID must match a factory in your native code
80+
request: const AdRequest(),
81+
listener: NativeAdListener(
82+
onAdLoaded: (ad) {
83+
_logger.info('Native Ad loaded successfully.');
84+
completer.complete(ad as NativeAd);
85+
},
86+
onAdFailedToLoad: (ad, error) {
87+
_logger.severe('Native Ad failed to load: $error');
88+
ad.dispose();
89+
completer.complete(null);
90+
},
91+
onAdClicked: (ad) {
92+
_logger.info('Native Ad clicked.');
93+
},
94+
onAdImpression: (ad) {
95+
_logger.info('Native Ad impression recorded.');
96+
},
97+
onAdClosed: (ad) {
98+
_logger.info('Native Ad closed.');
99+
ad.dispose();
100+
},
101+
onAdOpened: (ad) {
102+
_logger.info('Native Ad opened.');
103+
},
104+
onAdWillDismissScreen: (ad) {
105+
_logger.info('Native Ad will dismiss screen.');
106+
},
107+
),
108+
);
109+
110+
try {
111+
await ad.load();
112+
} catch (e) {
113+
_logger.severe('Error during native ad load: $e');
114+
completer.complete(null);
115+
}
116+
117+
return completer.future;
118+
}
119+
}

0 commit comments

Comments
 (0)