Flutter interstitial ad with AppOpen ads - flutter

Currently Im using native_admob_flutter for my flutter app. I also included AppOpenAds and Interstital ad within the app.
I want to display the AppOpen ad when user open the app/ resume to the app, here is how I do it in main.dart:
#override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.resumed:
loadAndDisplayAppOpenAd();
break;
case AppLifecycleState.inactive:
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.detached:
break;
}
}
However I found that displaying Interstial ad will also change the app state -> inactive -> paused -> resume, which trigger AppOpen ad.
This really create bad user experinence. Is there a way to overcome this? Thank you!

You may need a variable to make sure that app has been paused by user action, and not by interstitial.
static bool pausedByInterstitial = false;
...
class AppLifecycleReactor extends WidgetsBindingObserver {
final AppOpenAdManager appOpenAdManager;
AppLifecycleReactor({required this.appOpenAdManager});
static bool pausedByInterstitial = false;
#override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed && !pausedByInterstitial) {
appOpenAdManager.showAdIfAvailable();
}
}
}
...
showInter() {
AppLifecycleReactor.pausedByInterstitial = true;
InterstitialListener(
...
onAdHiddenCallback: (ad) {
AppLifecycleReactor.pausedByInterstitial = false;
},
...
);
}

Related

Call local_auth in flutter resume state will request authentication continuously

I'm using the local_auth package to lock app services after login if the user closes the app
what I want is to call the authentication function in the resume state of AppLifecycleState (when the user opens the app again)
but this prompt authentication infinitely
class _AppState extends State<App> with WidgetsBindingObserver {
#override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
#override
void didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.inactive:
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.resumed:
{
await _auth.authenticate(
useErrorDialogs: true,
stickyAuth: true,
);
}
break;
case AppLifecycleState.detached:
break;
}
}
#override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
This will call authentication forever
how to solve this?

adUnitId is not showing google banner ads in flutter

I'm trying to implement google ads through admob_flutter package, created adunitId through admob account. but these adUnitId are not showing ads.
when I'm using ca-app-pub-3940256099942544/2934735716 this adUnitId then it is showing test ads on google ad.
Here is my code:
import 'dart:async';
import 'dart:io';
import 'package:admob_flutter/admob_flutter.dart';
import 'package:flutter/material.dart';
class GoogleAdBanner extends StatefulWidget {
GoogleAdBanner(this.size);
final AdmobBannerSize size;
#override
State<StatefulWidget> createState() => BannerAdState();
}
class BannerAdState extends State<GoogleAdBanner> {
AdmobBanner _bannerAd;
final Completer<AdmobBanner> bannerCompleter = Completer<AdmobBanner>();
#override
void initState() {
super.initState();
Admob.requestTrackingAuthorization();
}
String getBannerAdUnitId() {
if (Platform.isIOS) {
return 'ca-app-pub-7282911616152886/6377817226';
} else if (Platform.isAndroid) {
return 'ca-app-pub-7282911616152886/8046122111';
}
return null;
}
void handleEvent(
AdmobAdEvent event, Map<String, dynamic> args, String adType) {
switch (event) {
case AdmobAdEvent.loaded:
print('New Admob $adType Ad loaded!');
break;
case AdmobAdEvent.opened:
print('Admob $adType Ad opened!');
break;
case AdmobAdEvent.closed:
print('Admob $adType Ad closed!');
break;
case AdmobAdEvent.failedToLoad:
print('Admob $adType failed to load. :(');
break;
default:
}
}
#override
Widget build(BuildContext context) {
return AdmobBanner(
adUnitId: getBannerAdUnitId(), //'ca-app-pub-3940256099942544/2934735716',
adSize: widget.size,
listener: (AdmobAdEvent event,
Map<String, dynamic> args) {
handleEvent(event, args, 'Banner');
},
onBannerCreated: (AdmobBannerController controller) {},
);
}
}
using it as
GoogleAdBanner(AdmobBannerSize.LARGE_BANNER)
Please help with this. Thanks in advance!
Real ads are displayed only when there are enough number of requests. The inventory is empty till the threshold has reached. If you are able to see the test ads you can be assured that the implementation is right. Make sure you change the ad app id in manifest and info.plist and add the right ad unit ad in ad request before release.
use getBannerAdUnitId() like this:-
String get BannerAdUnitId(){
//body
}
adUnitId: BannerAdUnitId(),

Flutter use WidgetsBindingObserver only when on top of the stack

I have a StatefulWidget and at the beginning of the State method's build I do
WidgetsBinding.instance
.addObserver(LifecycleEventHandler(resumeCallBack: () async {
if (mounted) {
refreshState();
}
}));
This is the code of the LifecycleEventHandler
class LifecycleEventHandler extends WidgetsBindingObserver {
final AsyncCallback resumeCallBack;
LifecycleEventHandler({
this.resumeCallBack,
});
#override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.resumed:
if (resumeCallBack != null) {
await resumeCallBack();
}
break;
case AppLifecycleState.inactive:
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.detached:
break;
}
}
}
the resumeCallBack is being called every time the app is resumed, even if the page is not visible to the user (if I navigate to another page without removing this from the stack).
Is there a way to run this callback only if the page is visible to the user? I don't want to dispose the state/page but just avoid unnecessary state updates

I need some functions call when app reopen

hai I need to call some function when app will reopen from sleep
after user can put app in minimize then when the reopen the app that function have to call in flutter
You need to listen to lifecycle events like this
class LifecycleEventHandler extends WidgetsBindingObserver {
final AsyncCallback resumeCallBack;
final AsyncCallback suspendingCallBack;
LifecycleEventHandler({
this.resumeCallBack,
this.suspendingCallBack,
});
#override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.resumed:
if (resumeCallBack != null) {
await resumeCallBack();
}
break;
case AppLifecycleState.inactive:
case AppLifecycleState.paused:
case AppLifecycleState.detached:
if (suspendingCallBack != null) {
await suspendingCallBack();
}
break;
}
}
}
class AppWidgetState extends State<AppWidget> {
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(
LifecycleEventHandler(resumeCallBack: () async => setState(() {
// here the app has resumed
}))
);
}
...
}

Show interstitial ad more than once admob

Ive been trying to display an interstitial ad several times during my app runtime. Ive read admob documentation about it and it seems that is a one-time-use object, I'd like to know if there's any workaround for this. Any help will be appreciated. Thanks
I know the question is about 8 years ago but I had the same problem. What worked for me:
import 'package:firebase_admob/firebase_admob.dart';
InterstitialAd _interstitialAd;
bool _isInterstitialAdReady;
void prepAd(){
initState();
_loadInterstitialAd();
}
void runAd() {
_interstitialAd.show();
}
void initState() {
_isInterstitialAdReady = false;
_interstitialAd = InterstitialAd(
adUnitId: InterstitialAd.testAdUnitId,
listener: _onInterstitialAdEvent,
);
}
void _loadInterstitialAd() {
_interstitialAd.load();
}
// TODO: Implement _onInterstitialAdEvent()
void _onInterstitialAdEvent(MobileAdEvent event) {
switch (event) {
case MobileAdEvent.loaded:
_isInterstitialAdReady = true;
break;
case MobileAdEvent.failedToLoad:
_isInterstitialAdReady = false;
print('Failed to load an interstitial ad');
break;
case MobileAdEvent.closed:
dispose();
prepAd();
break;
default:
// do nothing
}
}
void dispose() {// TODO: Dispose InterstitialAd object
_interstitialAd?.dispose();
}
I ran prepAd() once in the beginning and then just runAd() when I need it. You can check _isInterstitialAdReady == true inside runAd() if you want