Admob React Native google mobile ads (interstitial Ads)not working
I got this error- TypeError: undefined is not a function (near '...interstitial.onAdEvent...')
I am using- npm i react-native-google-mobile-ads
Thank you in advance for your support.
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 TestAds = ({ navigation }) => { useEffect(() => { let interstitial = InterstitialAd.createForAdRequest(TestIds.INTERSTITIAL, { requestNonPersonalizedAdsOnly: true, keywords: ['fashion', 'clothing'], }); let interstitialListener = interstitial.onAdEvent(type => { if (type === 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 TestAds
The below code is working please check if I still missed anything else
useEffect(() => { let interstitial = InterstitialAd.createForAdRequest(TestIds.INTERSTITIAL, { requestNonPersonalizedAdsOnly: true, keywords: ['fashion', 'clothing'], }); interstitial.addAdEventListener(AdEventType.LOADED, () => { interstitial.show(); }); interstitial.load(); return () => { interstitialListener = null; }; }, []);
write interstitial.addAdEventListener instead of interstitial.addAdEvent. you have set like above and follow Admob docs step by step.
interstitial.addAdEventListener(AdEventType.LOADED, () => { // do here what u want });https://docs.page/invertase/react-native-google-mobile-ads/displaying-ads
Thank you for your response. Last Night I tried the above docs code you shared it's worked. but it shows ads only one time. and when I click on the button then ads appear. actually, I want when users open or leave the page then ads should appear. I want the ads to display without clicking on button. please check the code I used, image link- i.sstatic.net/vFmbP.png
you have to call load function for ads in App.js file or in any app starting file, You must have ad loaded before showing, at same time load and show it does not work it needs some wait
Thank you so much sir it's working only when I open the page. could you please check my code if I still missed anything else- useEffect(() => { let interstitial = InterstitialAd.createForAdRequest(TestIds.INTERSTITIAL, { requestNonPersonalizedAdsOnly: true, keywords: ['fashion', 'clothing'], }); interstitial.addAdEventListener(AdEventType.LOADED, () => { interstitial.show(); }); interstitial.load(); return () => { interstitialListener = null; }; }, []);
I have updated and added the code in the question section, please ignore the comment section code because it would be hard to read the code in the comment section. Thank you Hamid Khan brother.
ok brother I will try your suggestions. Thank you.