广告加载失败:[错误:[googleMobileAds/error-code-no-fill] 广告请求成功,但由于广告库存不足,没有返回任何广告。]

问题描述 投票:0回答:1

我正在努力使用react-native-google-mobile-ads库将AdMob集成到我的React Native应用程序中。我已根据文档设置了所有内容,但遇到了一些问题。

设置 AdMob 帐户:我创建了一个 AdMob 帐户并获取了插页式广告和横幅广告的广告单元 ID。 应用程序 ID:将 AdMob 应用程序 ID 添加到 app.json 文件中。 代码 这是我设置插页式广告和横幅广告的代码的相关部分:

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

// Set up ad unit ID
const adUnitId = __DEV__ ? TestIds.INTERSTITIAL : 'ca-app-pub-7083880807910504/7362498272';

// Create an InterstitialAd instance
const interstitial = InterstitialAd.createForAdRequest(adUnitId, {
  keywords: ['fashion', 'clothing'],
});

const App = () => {
  const [loaded, setLoaded] = useState<boolean>(false);
  const [retryCount, setRetryCount] = useState<number>(0);
  const [maxRetries, setMaxRetries] = useState<number>(5);

  useEffect(() => {
    const loadAd = () => {
      interstitial.load();
    };

    const handleLoaded = () => {
      console.log('Ad Loaded');
      setLoaded(true);
      setRetryCount(0);
    };

    const handleError = (error: any) => {
      console.error('Ad Failed to Load:', error);
      if (error.code === 'no-fill' && retryCount < maxRetries) {
        const nextRetryCount = retryCount + 1;
        const delay = Math.min(30000, 1000 * 2 ** nextRetryCount);
        setRetryCount(nextRetryCount);
        setTimeout(loadAd, delay);
      } else {
        console.log('Ad failed to load after max retries');
      }
    };

    const handleClosed = () => {
      console.log('Ad Closed');
      setLoaded(false);
      loadAd();
    };

    const unsubscribeLoaded = interstitial.addAdEventListener(AdEventType.LOADED, handleLoaded);
    const unsubscribeError = interstitial.addAdEventListener(AdEventType.ERROR, handleError);
    const unsubscribeClosed = interstitial.addAdEventListener(AdEventType.CLOSED, handleClosed);

    loadAd();

    return () => {
      unsubscribeLoaded();
      unsubscribeError();
      unsubscribeClosed();
    };
  }, [retryCount, maxRetries]);

  const showInterstitial = () => {
    if (loaded) {
      interstitial.show();
      setLoaded(false);
    } else {
      console.log('Interstitial ad not loaded yet');
      Alert.alert('Ad Not Ready', 'Please try again later.');
    }
  };

  const showAdInspector = async () => {
    try {
      await MobileAds().openAdInspector();
    } catch (error: any) {
      console.error('Failed to open Ad Inspector:', error);
      Alert.alert('Error', 'Failed to open Ad Inspector.');
    }
  };

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Button title="Show Interstitial Ad" onPress={showInterstitial} />
      <Button title="Open Ad Inspector" onPress={showAdInspector} />
      <BannerAd
        unitId={__DEV__ ? TestIds.BANNER : 'ca-app-pub-7083880807910504/1234567890'}
        size={BannerAdSize.FULL_BANNER}
        requestOptions={{
          requestNonPersonalizedAdsOnly: true,
        }}
        onAdFailedToLoad={(error) => console.error('Banner Ad Failed to Load:', error)}
      />
    </View>
  );
};

export default App;

问题:横幅广告加载失败:[错误:[googleMobileAds/error-code-no-fill] 广告请求成功,但由于广告库存不足,没有返回任何广告。] 横幅广告:横幅广告没有展示,不确定是否是单位ID或设置有问题。 采取的步骤 已验证的广告单元 ID。 将 AdMob 应用 ID 添加到 app.json。 检查 AdMob 控制台中的广告单元设置是否正确。 有人可以帮我解决这些问题吗?任何有关如何解决 RequestOptions 错误并确保广告正确显示的建议将不胜感激。

提前致谢!

react-native admob
1个回答
0
投票

同样的问题。你修好了吗?我已经尝试了所有方法,但还没有成功 T.T

© www.soinside.com 2019 - 2024. All rights reserved.