Rainul Hassan
Rainul Hassan

Reputation: 61

React Native Admob Google Mobile Ads not working?

I copied the exact code from documentation and tried to check in my application but it's not working, here I just removed the button because I don't need the button.

Ads should appear automatically after users visit the page. Now when I run the app the ads do not appear.

What should I need to do? Where should I placed the- interstitial.show(); inside the code

import React, { useEffect, useState } from 'react';
import { Button } from 'react-native';
import { InterstitialAd, AdEventType, TestIds } from 'react-native-google-mobile-ads';

const adUnitId = __DEV__ ? TestIds.INTERSTITIAL : 'ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy';

const interstitial = InterstitialAd.createForAdRequest(adUnitId, {
  requestNonPersonalizedAdsOnly: true,
  keywords: ['fashion', 'clothing'],
});

function App() {
  const [loaded, setLoaded] = useState(false);

  useEffect(() => {
    const unsubscribe = interstitial.addAdEventListener(AdEventType.LOADED, () => {
      setLoaded(true);
    });

    // Start loading the interstitial straight away
    interstitial.load();

    // Unsubscribe from events on unmount
    return unsubscribe;
  }, []);

 
  if (!loaded) {
    return null;
  }

  return (
    
    <View>
<Text>Hello World</Text>
</View>
  );
}

Upvotes: 2

Views: 1721

Answers (2)

Ali Sina Yousofi
Ali Sina Yousofi

Reputation: 11

I think you should init the ads first(in your App.js):

import mobileAds from 'react-native-google-mobile-ads';
mobileAds()
.initialize()
.then(() => console.log('Ads initialized'))

Upvotes: 0

JosD
JosD

Reputation: 39

You need to have some kind of trigger for the ads, so if you wanted them to load automatically you would do so after the ad is loaded. Before even getting to this point, you also need to make sure that you have followed all of the installation steps, including await mobileAds().initialize(); before you load the interstitial ads.

useEffect(() => {
const unsubscribe = interstitial.addAdEventListener(AdEventType.LOADED, () => {
  setLoaded(true);
});

// Start loading the interstitial straight away
interstitial.load();

// Include the .show() function here
interstitial.show();


// Unsubscribe from events on unmount
return unsubscribe;
}, []);

Upvotes: 1

Related Questions