firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.68k stars 3.97k forks source link

Flutter firebase_admob show only once #464

Closed kroikie closed 4 years ago

kroikie commented 5 years ago

The following code show the banner ad at the bottom of the application and show interstitial ad when the button is clicked. No problem with banner ad. The problem is, when the app start and click button for the first time, the interstitial is shown but the interstitial is not shown again starting from second time the button is clicked. There are no errors and all log message show success. Something wrong in my code? I'm testing on android device. And also posted this question here but get no answers yet.

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');
                }
              });
            },
          ),
        ),
      ),
    );
  }
}
kroikie commented 5 years ago

@developernca

The issue at https://github.com/flutter/flutter/issues/27520 has been closed and moved here. Future collaboration on this issue will be done here.

iapicca commented 5 years ago

https://github.com/FirebaseExtended/flutterfire/issues/135 https://github.com/FirebaseExtended/flutterfire/issues/63 https://github.com/FirebaseExtended/flutterfire/issues/458

BondarenkoStas commented 4 years ago

Closing in favour of https://github.com/FirebaseExtended/flutterfire/issues/278 to avoid duplication. Please follow it there and upvote.