-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with AppOpen Ads #404
Comments
Hi @DabPb, Thanks for filing the issue. Could you please share the minimal and complete code sample along with the output of Thanks |
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:googlemobileadverts/app_lifecycle_reactor.dart';
import 'app_open_ad_manager.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(const MyApp());
}
const String testDevice = 'YOUR_DEVICE_ID';
const int maxFailedLoadAttempts = 3;
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static final AdRequest request = AdRequest(
keywords: <String>['foo', 'bar'],
contentUrl: 'http://foo.com/bar.html',
nonPersonalizedAds: true,
);
InterstitialAd? _interstitialAd;
int _numInterstitialLoadAttempts = 0;
@override
void initState() {
super.initState();
_createInterstitialAd();
AppOpenAdManager appOpenAdManager = AppOpenAdManager()..loadAd();
WidgetsBinding.instance!
.addObserver(AppLifecycleReactor(appOpenAdManager: appOpenAdManager));
}
void _createInterstitialAd() {
InterstitialAd.load(
adUnitId: InterstitialAd.testAdUnitId,
request: request,
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (InterstitialAd ad) {
print('$ad loaded');
_interstitialAd = ad;
_numInterstitialLoadAttempts = 0;
_interstitialAd!.setImmersiveMode(true);
},
onAdFailedToLoad: (LoadAdError error) {
print('InterstitialAd failed to load: $error.');
_numInterstitialLoadAttempts += 1;
_interstitialAd = null;
if (_numInterstitialLoadAttempts <= maxFailedLoadAttempts) {
_createInterstitialAd();
}
},
));
}
void _showInterstitialAd() {
if (_interstitialAd == null) {
print('Warning: attempt to show interstitial before loaded.');
return;
}
_interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
onAdShowedFullScreenContent: (InterstitialAd ad) =>
print('ad onAdShowedFullScreenContent.'),
onAdDismissedFullScreenContent: (InterstitialAd ad) {
print('$ad onAdDismissedFullScreenContent.');
ad.dispose();
_createInterstitialAd();
},
onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) {
print('$ad onAdFailedToShowFullScreenContent: $error');
ad.dispose();
_createInterstitialAd();
},
);
_interstitialAd!.show();
_interstitialAd = null;
}
@override
void dispose() {
super.dispose();
_interstitialAd?.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Builder(builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('AdMob Demo'),
),
body: SafeArea(
child: Center(
child: OutlinedButton(
onPressed: () => _showInterstitialAd(),
child: const Text('View Interstitial Ads'))),
),
);
}),
);
}
}
|
Any Luck? |
I have the same problem. |
Same issue! :( |
Hi @DabPb thanks for the code sample, I can reproduce the issue using the latest Screen.Recording.2021-10-22.at.3.55.26.PM.movflutter doctor -v
|
the problem is interstitial popping up is considered as native android element and it causes flutter app to change its lifecycle to inactive and after you close the native view interstitial the flutter app come back to life and it changes its lifecycle to resumed so your code calls the show app open ads immediately. As a work around for this ill suggest you to use this plugin : flutter_fgbg and then you can listen to the app if its on background or foreground and show the app open ad when app on foreground
|
Closing as fixed in #425 |
Plugin Version : 0.13.5
When we show Interstitial ads, App open ad is also displayed each time. it means display twice ads 1 interstitial and another one is App open. some times it going to in infinite loop, App open ads not close.
The text was updated successfully, but these errors were encountered: