Search code examples
reactjsreact-nativeadmobgoogle-ads-api

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

Solution

  • 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