firebase_admob_config 1.3.1 firebase_admob_config: ^1.3.1 copied to clipboard
Control google admob by firebase remote config. This package help you can config AdMob ads with Firebase Remote Config and A/B testing on them.
import 'dart:convert';
import 'dart:developer';
import 'package:firebase_admob_config/firebase_admob_config.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await _setupFirebaseRemoteConfig();
await MobileAds.instance.initialize();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
//
// Interstitial Ads from Firebase Remote Config
final interstitialAd = AppInterstitialAd.fromKey(
configKey: 'interstitial_ad',
);
// Rewarded Ads from Firebase Remote Config
final rewardedAd = AppRewardedAd.fromKey(
configKey: 'rewarded_ad',
);
// Rewarded Interstitial Ads from Firebase Remote Config
final rewardedInterstitialAd = AppRewardedInterstitialAd.fromKey(
configKey: 'rewarded_interstitial_ad',
);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Admob with Firebase Remote Config'),
//
// Banner Ads from Firebase Remote Config
AppBannerAd.fromKey(configKey: 'banner_ad'),
TextButton(
child: const Text('Show InterstitialAd'),
onPressed: () => interstitialAd.run(),
),
TextButton(
child: const Text('Show RewardedAd'),
onPressed: () => rewardedAd.show(onUserEarnedReward: (ad, rw) {
log('$ad ${rw.type} ${rw.amount}', name: 'MyApp.RewardedAd');
}),
),
TextButton(
child: const Text('Show RewardedInterstitialAd'),
onPressed: () => rewardedInterstitialAd.show(
onUserEarnedReward: (ad, rw) {
log('$ad ${rw.type} ${rw.amount}',
name: 'RewardedInterstitial');
},
),
),
],
),
),
),
);
}
}
// Just setup Firebase remote config
Future<void> _setupFirebaseRemoteConfig() async {
await Firebase.initializeApp();
final FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.instance;
await remoteConfig.setConfigSettings(RemoteConfigSettings(
fetchTimeout: const Duration(seconds: 10),
minimumFetchInterval: const Duration(hours: 10),
));
await remoteConfig.setDefaults(_defaultAdmobConfig);
}
/*
Config width, height by following ads size
The standard banner (320x50) size.
The large banner (320x100) size.
The medium rectangle (300x250) size.
The full banner (468x60) size.
The leaderboard (728x90) size.
*/
final Map<String, dynamic> _defaultAdmobConfig = {
'banner_ad': jsonEncode({
"enable": true,
"ad_unit_id_android": "ca-app-pub-3940256099942544/6300978111",
"ad_unit_id_ios": "ca-app-pub-3940256099942544/2934735716",
"position": null,
"distance": null,
"width": 300,
"height": 250
}),
'interstitial_ad': jsonEncode({
"enable": true,
"ad_unit_id_android": "ca-app-pub-3940256099942544/1033173712",
"ad_unit_id_ios": "ca-app-pub-3940256099942544/4411468910",
"request_time_to_show": 3,
"fail_time_to_stop": 3,
"init_request_time": 0
}),
'rewarded_ad': jsonEncode({
"enable": true,
"ad_unit_id_android": "ca-app-pub-3940256099942544/5224354917",
"ad_unit_id_ios": "ca-app-pub-3940256099942544/1712485313",
}),
'rewarded_interstitial_ad': jsonEncode({
"enable": true,
"ad_unit_id_android": "ca-app-pub-3940256099942544/5354046379",
"ad_unit_id_ios": "ca-app-pub-3940256099942544/6978759866",
})
};