Reputation: 1215
The following code show the banner ad at the bottom of the application and show interstitial ad when click. Interstitial ad is shown when the button is pressed. The problem is, when the app start and click button for the first time, the interstitial is shown but the interstitial is not shown starting from second time. There are no errors and all log message show success. Is my code error? I'm testing on android device.
import 'package:firebase_admob/firebase_admob.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final admobAppId = 'ca-app-pub-3940256099942544~3347511713';
final bannerId = 'ca-app-pub-3940256099942544/6300978111';
final interstitialId = 'ca-app-pub-3940256099942544/1033173712';
BannerAd bannerAd;
InterstitialAd interstitialAd;
MyApp() {
FirebaseAdMob.instance.initialize(appId: admobAppId);
makeBannerAd();
initInterstitialAd();
}
makeBannerAd() {
bannerAd = BannerAd(
adUnitId: bannerId,
size: AdSize.smartBanner,
listener: (MobileAdEvent me) {
print('MobileAdEvent $me');
});
bannerAd
..load()
..show();
}
initInterstitialAd() {
interstitialAd = InterstitialAd(
adUnitId: interstitialId,
listener: (MobileAdEvent me) {
print(
'========== Interstitial ad mobile ad event =========== \n $me');
if (me == MobileAdEvent.closed) {
print('Interstitial closed');
loadInterstitialAd();
}
});
loadInterstitialAd();
}
loadInterstitialAd() {
interstitialAd.load().then((val) {
if (val) {
print('Interstitial ad loaded callback success');
} else {
print('Interstitial ad loaded callback failed');
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('AdMob plugin test'),
),
body: Center(
child: RaisedButton(
child: Text('Show Interstitial!'),
onPressed: () {
interstitialAd.show().then((val) {
if (val) {
print('Interstitial ad show callback success');
} else {
print('Interstitial ad show callback fail');
}
});
},
),
),
),
);
}
}
Update
Following is the code with dispose(). It show up to 2 times.
class MyApp extends StatelessWidget {
final admobAppId = 'ca-app-pub-3940256099942544~3347511713';
final bannerId = 'ca-app-pub-3940256099942544/6300978111';
final interstitialId = 'ca-app-pub-3940256099942544/1033173712';
BannerAd bannerAd;
InterstitialAd interstitialAd;
MobileAdTargetingInfo mobileAdTargetingInfo;
MyApp() {
FirebaseAdMob.instance.initialize(appId: admobAppId);
makeBannerAd();
initInterstitialAd();
}
makeBannerAd() {
bannerAd = BannerAd(
adUnitId: bannerId,
size: AdSize.smartBanner,
listener: (MobileAdEvent me) {
print('Banner => MobileAdEvent $me');
});
bannerAd
..load()
..show();
}
initInterstitialAd() {
interstitialAd = InterstitialAd(
adUnitId: interstitialId,
listener: (MobileAdEvent me) {
print(
'========== Interstitial ad mobile ad event =========== $me');
if (me == MobileAdEvent.closed) {
print('Interstitial closed');
interstitialAd.dispose().then((val){
if(val){
loadInterstitialAd();
}else{
}
});
} else if (me == MobileAdEvent.failedToLoad) {
print('Interstitial failed to load');
loadInterstitialAd();
}
});
loadInterstitialAd();
}
loadInterstitialAd() {
interstitialAd.load();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('AdMob plugin test'),
),
body: Center(
child: RaisedButton(
child: Text('Show Interstitial!'),
onPressed: () {
interstitialAd.show();
},
),
),
),
);
}
}
Upvotes: 5
Views: 7032
Reputation: 1186
May be am late here but this might help for future viewers. Here is the most efficient and best method to this, so that you can watch ads one after another. For all types of ads, here I have given examples of Interstitial and Rewarded, the same logic for others. Also here you can find how to load the ad again after it failed.
Full code:
InterstitialAd _interstitialAd;
RewardedAd _rewardedAd;
void interstitialAdload () {
InterstitialAd.load(
adUnitId: '<test ad>',
request: AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (InterstitialAd ad) {
// Keep a reference to the ad so you can show it later.
this._interstitialAd = ad;
ad.fullScreenContentCallback = FullScreenContentCallback(
onAdShowedFullScreenContent: (InterstitialAd ad) =>
print('$ad onAdShowedFullScreenContent.'),
onAdDismissedFullScreenContent: (InterstitialAd ad) {
print('$ad onAdDismissedFullScreenContent.');
ad.dispose();
},
onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) {
print('$ad onAdFailedToShowFullScreenContent: $error');
ad.dispose();
},
onAdImpression: (InterstitialAd ad) {
interstitialAdload(); // <<<-------------- LOAD AD AGAIN WHEN THE USER WATCHES IT SO THAT ITS READY TO BE WATCHED JUST AFTER THE IMPRESSION OCCURS
print('$ad impression occurred.');
}
);
},
onAdFailedToLoad: (LoadAdError error) {
interstitialAdload(); // <<<-------------- LOAD AD AGAIN IF IT FAILS
print('InterstitialAd failed to load: $error');
},
));
}
void rewardedAdLoad() {
RewardedAd.load(
adUnitId: '<test ad>',
request: AdRequest(),
rewardedAdLoadCallback: RewardedAdLoadCallback(
onAdLoaded: (RewardedAd ad) {
print('$ad loaded.');
// Keep a reference to the ad so you can show it later.
this._rewardedAd = ad;
ad.fullScreenContentCallback = FullScreenContentCallback(
onAdShowedFullScreenContent: (RewardedAd ad) =>
print('$ad onAdShowedFullScreenContent.'),
onAdDismissedFullScreenContent: (RewardedAd ad) {
print('$ad onAdDismissedFullScreenContent.');
ad.dispose();
},
onAdFailedToShowFullScreenContent: (RewardedAd ad, AdError error) {
print('$ad onAdFailedToShowFullScreenContent: $error');
ad.dispose();
},
onAdImpression: (RewardedAd ad)
{
print('$ad impression occurred.');
rewardedAdLoad(); // <<<-------------- LOAD AD AGAIN WHEN THE USER WATCHES IT SO THAT ITS READY TO BE WATCHED JUST AFTER THE IMPRESSION OCCURS
}
);
},
onAdFailedToLoad: (LoadAdError error) {
rewardedAdLoad(); // <<<-------------- LOAD AD AGAIN IF IT FAILS
print('RewardedAd failed to load: $error');
},
)
);
}
void showInterstitialAd() {
_interstitialAd.show();
}
void showRewardedAd() {
_rewardedAd.show(onUserEarnedReward: (RewardedAd ad, RewardItem rewardItem) {
// Reward the user for watching an ad.
reward();
});
}
Upvotes: 0
Reputation: 157
Leave an example of the way I work. I hope it helps.
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Intersticial',
navigatorKey: Get.key, // use get: ^1.6.3
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Intersticial'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
InterstitialAd _interstitialAd;
InterstitialAd createIntersticialAd(){
return InterstitialAd(
adUnitId: AD_MOB_AD_INTERSTICIAL_1_ID,
targetingInfo: AdMobUtil.targetingInfo,
listener: (MobileAdEvent event) {
print("InterstitialAd event is $event");
// Dispose InterstitialAd
if (event == MobileAdEvent.closed)
dispose();
},
);
}
@override
void dispose() {
//
// dispose intersticialAd
//
_interstitialAd?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Go to second page',
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
//
// create intersticialAd and send by parameter
//
_interstitialAd ??= createIntersticialAd();
Get.to(SecondPage(interstitialAd: _interstitialAd,));
},
tooltip: 'Go to Second page',
child: Icon(Icons.arrow_forward),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class SecondPage extends StatefulWidget {
//
// define intersticial final
//
final InterstitialAd interstitialAd;
const SecondPage({Key key, this.interstitialAd}) : super(key: key);
@override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
@override
Widget build(BuildContext context) {
return Container();
}
@override
void dispose() {
//
// show intersticial ad
//
widget.interstitialAd..load()..show();
super.dispose();
}
}
The banner will open when you return from the second page.
I used this package for navigation: https://pub.dev/packages/get
If helped mark as useful ..;)
Upvotes: 0
Reputation: 121
I had the same issue and resolved it with the InterstitialAd object as shown:
InterstitialAd myInterstitial() {
return InterstitialAd(
adUnitId: InterstitialAd.testAdUnitId,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
if (event == MobileAdEvent.failedToLoad) {
interstitialAd..load();
} else if (event == MobileAdEvent.closed) {
interstitialAd = myInterstitial()..load();
}
},
);
}
@override
void initState() {
FirebaseAdMob.instance.initialize(appId:
FirebaseAdMob.testAppId);
interstitialAd = myInterstitial()..load();
super.initState();
}
@override
void dispose() {
interstitialAd?.dispose();
super.dispose();
}
Call method:
interstitialAd
..load()
..show();
From my understanding, you can use the listener to recursively call the initial InterstitialAd object when the event is closed. My source is from Tensor Programming Youtube Channel He explains it much better than me.
Upvotes: 12
Reputation: 1215
I modified and fixed the code as follow and I also posted in the github issue. Now, whenever the button is pressed the ad is shown. I tested 10+ times and it work well. It seem InterstitialAd need to create every time the ad is wanted to show. But I don't know exactly. If there is someone who knows why, please explain me.
import 'package:flutter/material.dart';
import 'package:firebase_admob/firebase_admob.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
BannerAd myBanner;
MyApp() {
FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
myBanner =
BannerAd(adUnitId: BannerAd.testAdUnitId, size: AdSize.smartBanner);
myBanner
..load()
..show();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Interstitial Ad test"),
),
body: Center(
child: RaisedButton(
child: Text("Interstitial ad"),
onPressed: () {
InterstitialAd interstitialAd = new InterstitialAd(
adUnitId: InterstitialAd.testAdUnitId,
listener: (MobileAdEvent e) {
print("Mobile ad event => $e");
});
interstitialAd.load().then((val) {
interstitialAd.show();
});
},
),
),
),
);
}
}
Upvotes: 2
Reputation: 1197
I faced the same problem. The solution is simple.
Just use interstitialAd.dispose()
to dispose the ad so that a new interstitial ad get loaded the next time.
Upvotes: 1