Skip to content
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

Closed
DabPb opened this issue Oct 14, 2021 · 8 comments
Closed

Issue with AppOpen Ads #404

DabPb opened this issue Oct 14, 2021 · 8 comments
Assignees
Labels
app open ad Issues related to App Open ads bug Something isn't working e2-days Effort: < 5 days fixed Issue has been resolved and pull request linked p2-medium

Comments

@DabPb
Copy link

DabPb commented Oct 14, 2021

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.

@maheshmnj maheshmnj added the in triage Issue currently being evaluated label Oct 14, 2021
@maheshmnj
Copy link
Collaborator

Hi @DabPb, Thanks for filing the issue. Could you please share the minimal and complete code sample along with the output of flutter doctor -v to better address the issue.

Thanks

@maheshmnj maheshmnj added the feedback required Further information is requested label Oct 14, 2021
@DabPb
Copy link
Author

DabPb commented Oct 15, 2021

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'))),
          ),
        );
      }),
    );
  }
}

[✓] Flutter (Channel stable, 2.5.2, on macOS 11.5.2 20G95 darwin-x64, locale en-US)
• Flutter version 2.5.2 at /Users/pradipdabhi/Library/Android/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 3595343e20 (2 weeks ago), 2021-09-30 12:58:18 -0700
• Engine revision 6ac856380f
• Dart version 2.14.3

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at /Users/pradipdabhi/Library/Android/sdk
• Platform android-31, build-tools 31.0.0
• ANDROID_HOME = /Users/pradipdabhi/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.5.1, Build version 12E507
• CocoaPods version 1.11.0

[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] Connected device (2 available)
• Redmi 6A (mobile) • 56.223.97.183:5555 • android-arm • Android 9 (API 28)
• Chrome (web) • chrome • web-javascript • Google Chrome 94.0.4606.81

• No issues found!

@DabPb
Copy link
Author

DabPb commented Oct 18, 2021

Any Luck?

@devmoonho
Copy link

I have the same problem.
Appopen ad appears after interstitial ad impression
In the opposite case, the same symptoms.

@codetenacity
Copy link

Same issue! :(

@jjliu15 jjliu15 self-assigned this Oct 20, 2021
@maheshmnj
Copy link
Collaborator

Hi @DabPb thanks for the code sample, I can reproduce the issue using the latest google_mobile_ads: ^0.13.5

Screen.Recording.2021-10-22.at.3.55.26.PM.mov
flutter doctor -v
[✓] Flutter (Channel stable, 2.5.2, on macOS 11.6 20G165 darwin-arm, locale en-GB)
    • Flutter version 2.5.2 at /Users/mahesh/Documents/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 3595343e20 (11 days ago), 2021-09-30 12:58:18 -0700
    • Engine revision 6ac856380f
    • Dart version 2.14.3

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/mahesh/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 13.0, Build version 13A233
    • CocoaPods version 1.10.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)

[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 60.1.4
    • Dart plugin version 212.5080.8

[✓] VS Code (version 1.59.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.27.0

[✓] Connected device (4 available)
    • sdk gphone arm64 (mobile) • emulator-5554                        • android-arm64  • Android 11 (API 30) (emulator)
    • iPhone 11 Pro (mobile)    • 3209EA5A-2ABF-46BE-8C94-83AAF6EF59FB • ios            • com.apple.CoreSimulator.SimRuntime.iOS-13-1 (simulator)
    • macOS (desktop)           • macos                                • darwin-arm64   • macOS 11.6 20G165 darwin-arm
    • Chrome (web)              • chrome                               • web-javascript • Google Chrome 94.0.4606.71

• No issues found!
[✓] Flutter (Channel master, 2.6.0-12.0.pre.299, on macOS 11.6 20G165 darwin-arm, locale en-GB)
    • Flutter version 2.6.0-12.0.pre.299 at /Users/mahesh/Documents/flutter_master
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 6d5fc420b6 (2 days ago), 2021-10-11 21:23:02 -0700
    • Engine revision 7a666ecc6e
    • Dart version 2.15.0 (build 2.15.0-181.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/mahesh/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.10.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)

[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 60.1.4
    • Dart plugin version 212.5080.8

[✓] VS Code (version 1.59.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.27.0

[✓] Connected device (5 available)
    • Redmi K20 Pro (mobile) • 192.168.1.2:5555                     • android-arm64  • Android 11 (API 30)
    • iPhone 12 Pro (mobile) • B622CBB7-F906-4FA7-8F49-FACEAAC905AB • ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
    • iPhone 12 Pro (mobile) • F4DD7611-6C3F-4E04-B1BD-18ACA85F14AE • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-0 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64   • macOS 11.6 20G165 darwin-arm
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome 94.0.4606.81

• No issues found!

@maheshmnj maheshmnj added app open ad Issues related to App Open ads bug Something isn't working e2-days Effort: < 5 days p2-medium and removed feedback required Further information is requested in triage Issue currently being evaluated labels Oct 22, 2021
@elkhalifte
Copy link

elkhalifte commented Oct 29, 2021

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
https://pub.dev/packages/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

late StreamSubscription<FGBGType> subscription;
AppOpenAdManager appOpenAdManager = AppOpenAdManager()..loadAd();

 @override
  void initState() {
    subscription = FGBGEvents.stream.listen((event) {
      print(event);
      if (event == FGBGType.foreground) {
        appOpenAdManager.showAdIfAvailable();
      }
      
    });
}

jjliu15 added a commit to jjliu15/googleads-mobile-flutter-1 that referenced this issue Nov 5, 2021
jjliu15 added a commit that referenced this issue Nov 9, 2021
@maheshmnj
Copy link
Collaborator

Closing as fixed in #425

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
app open ad Issues related to App Open ads bug Something isn't working e2-days Effort: < 5 days fixed Issue has been resolved and pull request linked p2-medium
Projects
None yet
Development

No branches or pull requests

6 participants