Posts tagged with google-mobile-ads

i am using google_mobile_ads: ^5.2.0 to display adds on my flutter app. I need to request and display 3 different sizes of banner ads from google.

This ad is supposed to be show in a scrollable column so i must set the height of it, the problem is that when the add is loaded i don't have the information about the loaded ad, it could be any of the three. If i don't set the height it is either hidden or there is an error that the height cannot be infinite.

note: in the snippet in only set the temporary size, because i don't know how to get the correct one.

 Future<void> loadAd() async {     _bannerAd = AdManagerBannerAd(         adUnitId: adUnitId,         request: const AdManagerAdRequest(),         sizes: [           const AdSize(width: 300, height: 100),           const AdSize(width: 300, height: 250),           const AdSize(width: 300, height: 600)         ],         listener: AdManagerBannerAdListener(           onAdLoaded: (ad) {             if (!mounted) {               ad.dispose();               return;             }             debugPrint('$ad loaded.');             setState(() {               _isLoaded = true;             });           },           onAdFailedToLoad: (ad, error) {             debugPrint('AdManagerBannerAd failed to load: $error');             ad.dispose();           },         ))       ..load();   }   @override   void dispose() {          _bannerAd?.dispose();     super.dispose();   }   @override   Widget build(BuildContext context) {     return _isLoaded && _bannerAd != null         ? Align(             alignment: Alignment.bottomCenter,             child: SafeArea(               child: SizedBox(                 width: _bannerAd!.sizes[0].width.toDouble(),                 height: _bannerAd!.sizes[0].height.toDouble(),                 child: AdWidget(ad: _bannerAd!),               ),             ),           )         : const SizedBox             .shrink();    } 

please help.

How can I implement InterstitialAd ads when a user closes the currently visited page.

A user also can go back(close a page) from a page using his mobile device back button(not react native back header button).

Please check the below code

import React, { useState, useEffect } from "react"; import { View, Button, Text, ScrollView, } from 'react-native'; import { AppOpenAd, InterstitialAd, RewardedAd, BannerAd, TestIds, AdEventType } from 'react-native-google-mobile-ads'; const Test = ({ navigation }) => { useEffect(() => {         let interstitial = InterstitialAd.createForAdRequest(TestIds.INTERSTITIAL, {             requestNonPersonalizedAdsOnly: true,             keywords: ['fashion', 'clothing'],         });         interstitial.addAdEventListener(AdEventType.LOADED, () => {             interstitial.show();         });         interstitial.load();         return () => {             interstitialListener = null;         };     }, []); return (         <ScrollView>             <View>                 <View style={{ marginTop: 20 }}>                     <Text>                         Lorem Ipsum is simply dummy text of the printing and typesetting industry.                         Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, </Text>                 </View>             </View>         </ScrollView>     ) } export default Test 

MyApp.js file

import * as React from 'react'; import { NavigationContainer } from '@react-navigation/native'; import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; import Test from './Test'; import TestTwo from './TestTwo'; const Tab = createBottomTabNavigator(); const HomeTabs = ({ navigation }) =>{   return (     <Tab.Navigator screenOptions={{ headerShown: false }}>       <Tab.Screen name="Test" component={Test} />      </Tab.Navigator>   ); } const Stack = createNativeStackNavigator(); export default function App () {   return (     <NavigationContainer>       <Stack.Navigator>         <Stack.Screen name="Home" component={HomeTabs} />         <Tab.Screen name="TestTwo" component={TestTwo} />       </Stack.Navigator>     </NavigationContainer>   ); } 

In podfile.lock I have:

  • Google-Mobile-Ads-SDK (8.7.0):
    • GoogleAppMeasurement (< 9.0, >= 7.0)
    • GoogleUserMessagingPlatform (>= 1.1)

This code works fine, so the SDK installed successfully.

import GoogleMobileAds  let options = GADMultipleAdsAdLoaderOptions()  options.numberOfAds = 5  adLoader = GADAdLoader(adUnitID: adUnitID, rootViewController: requester, adTypes: [.native], options: [options])  adLoader.delegate = self 

But I can display the received GADNativeAd, because views are unaccessible.

import UIKit import GoogleMobileAds class FeedAdTVC: UITableViewCell {     func setup(with: GADNativeAd) {         let templateView = GADUnifiedNativeAdView()     } } 

I got: Cannot find 'GADUnifiedNativeAdView' in scope

I recently integrated google_mobile_ads plugin for Flutter after firebase_admob plugin got deprecated. Ever since then my Rewarded Ads stopped working. These are the errors I get:

(13077): This request is sent from a test device. E/chromium(13077): [ERROR:cookie_manager.cc(137)] Strict Secure Cookie policy does not allow setting a secure cookie for http://googleads.g.doubleclick.net/ for apps targeting >= R. Please either use the 'https:' scheme for this URL or omit the 'Secure' directive in the cookie value. W/Ads (13077): #004 The webview is destroyed. Ignoring action.

My code is as below:

void main() {   WidgetsFlutterBinding.ensureInitialized();   MobileAds.instance.initialize();   InAppPurchaseConnection.enablePendingPurchases();   runApp(MyApp()); } class _RewardedVideoState extends State<RewardedVideo>{   bool _rewardedReady = false;   RewardedAd _rewardedAd;   static final AdRequest _adRequest = AdRequest(     keywords: <String>['Puzzles', 'Games', 'Word Games'],     nonPersonalizedAds: true,   );   @override   void didChangeDependencies() {     createRewardedAd();     super.didChangeDependencies();   }   void createRewardedAd([Score userScore]) {     print('Inside createRewardedAd');     // RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("CFA70A4A1BD59DA3323D586CA8BD2541"))     _rewardedAd = RewardedAd(       adUnitId: RewardedAd.testAdUnitId,       request: _adRequest,       listener: AdListener(           onAdLoaded: (Ad ad) {             print('${ad.runtimeType} loaded. RADHA ');             _rewardedReady = true;           },           onAdFailedToLoad: (Ad ad, LoadAdError error) {             print('${ad.runtimeType} failed to load: $error');             ad.dispose();             _rewardedAd = null;             createRewardedAd(userScore);           },           onAdOpened: (Ad ad) => print('${ad.runtimeType} onAdOpened.'),           onAdClosed: (Ad ad) {             print('${ad.runtimeType} closed.');             ad.dispose();             createRewardedAd(userScore);           },           onApplicationExit: (Ad ad) =>               print('${ad.runtimeType} onApplicationExit.'),           onRewardedAdUserEarnedReward: (RewardedAd ad, RewardItem reward) {             userScore.updateHintsEarned(reward.amount);           }),     )..load();     print('Completed RewardedAd Load ' + _rewardedAd.toString());   }   @override   void dispose() {     _rewardedAd.dispose();     super.dispose();   }   @override   Widget build(BuildContext context) {     final Score userScore = Provider.of<Score>(context, listen: false);     print('Inside RewardedVideo widget *** ........');     try {       if (_rewardedReady) {         print('Showing rewardedAd ***');         _rewardedAd.show();         _rewardedReady = false;         _rewardedAd = null;       } else         createRewardedAd(userScore);     } catch (e) {       print("error in showing ad: " + e.toString());     }     return SizedBox(       height: 0,     );   } } 

I am able to get banner ads (not included in this code) but rewarded ad does not load at all. Any idea what might be going wrong here?