Reputation: 69
I want to show and App open Ad when the app again resume from the background but the ad is not showing. Please solve this issue in this page.To show an AppOpenAd when your Flutter app moves from the background to the resumed state using google_mobile_ads: ^3.0.0s . Perform all code in this page and the ad show on BottomNavigationPAge(), the AppOpenAd is show only for the first time . Thank you in Advance.
class AppLifecycleReactor {
final AppOpenAdManager appOpenAdManager;
AppLifecycleReactor({required this.appOpenAdManager});
void listenToAppStateChanges() {
AppStateEventNotifier.startListening();
AppStateEventNotifier.appStateStream
.forEach((state) => _onAppStateChanged(state));
}
void _onAppStateChanged(AppState appState) {
if (appState == AppState.foreground ){
appOpenAdManager.showAdIfAvailable();
appOpenAdManager.loadAppOpenAd();
}
}
}
class AppOpenAdManager {
AppOpenAd? appOpenAd;
loadAppOpenAd() {
AppOpenAd.load(
adUnitId: "ca-app-pub-3940256099942544/3419835294",
request: const AdRequest(),
adLoadCallback: AppOpenAdLoadCallback(
onAdLoaded: (ad) {
appOpenAd = ad;
appOpenAd!.show();
},
onAdFailedToLoad: (error) {
print(error);
},
),
orientation: AppOpenAd.orientationPortrait,
);
}
void showAdIfAvailable() {
if (appOpenAd != null) {
appOpenAd!.show();
}
}
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
AppOpenAdManager();
final cameras = await availableCameras();
final firstCamera = cameras.first;
bool beepChecked = true;
final appOpenAdManager = AppOpenAdManager();
appOpenAdManager.loadAppOpenAd();
final SharedPreferences prefs = await SharedPreferences.getInstance();
final bool shown = prefs.getBool('carouselShown') ?? false;
final String initialRoute = shown ? '/' : '/carousel';
runApp(
ChangeNotifierProvider(
create: (_) => SettingsProvider(),
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: "QR Code Scanner",
theme: ThemeData(primaryColor: Colors.lightBlue.shade300),
// home: initialRouteWidget,
initialRoute: initialRoute,
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => const BottomNavigationBarPage(),
'/carousel': (BuildContext context) => CarouselWithBackground(
onCarouselCompleted: () async {
// Mark the carousel as shown when it is completed
await prefs.setBool('carouselShown', true);
Navigator.of(context).pushReplacementNamed('/');
},
),
'/TakePictureScreen': (BuildContext context) =>
TakePictureScreen(camera: firstCamera),
'/about': (BuildContext context) => AboutPage(),
'/rate': (BuildContext context) => RateUsPage(),
'/feedback': (BuildContext context) => FeedbackPage(),
'/drawer': (BuildContext context) => SideMenuDrawer(),
},
),
),
);
}
Upvotes: 1
Views: 388
Reputation: 1
you should to take a look in https://github.com/googleads/googleads-mobile-flutter/tree/main/samples/admob/app_open_example I think you forget calling AppLifecycleReactor in your main()
Upvotes: 0