这个问题通常是由于广告请求过程中出现错误导致的。可以在RewardedAd对象中添加一个事件监听器来捕获错误,并在出错时执行相应操作,例如显示一个默认的广告或重试请求。以下是一个例子:
import React, { useState, useEffect } from "react";
import { Text, View } from "react-native";
import { RewardedAd, RewardedAdEventType } from "@react-native-firebase/admob";
const rewardedAdUnitId = "";
function App() {
const [adLoaded, setAdLoaded] = useState(false);
const [adError, setAdError] = useState(null);
useEffect(() => {
const rewardedAd = RewardedAd.createForAdRequest(rewardedAdUnitId, {
requestNonPersonalizedAdsOnly: true,
});
const eventListener = rewardedAd.onAdEvent((type, error, reward) => {
if (type === RewardedAdEventType.LOADED) {
setAdLoaded(true);
} else if (type === RewardedAdEventType.ERROR) {
setAdError(error);
}
});
rewardedAd.load();
return () => {
eventListener();
rewardedAd?.destroy();
};
}, []);
if (adError) {
return (
Failed to load ad: {adError.message || adError}
);
}
return (
{!adLoaded && Loading ad... }
{adLoaded && Ad loaded successfully! }
);
}
export default App;
在上面的代码中,我们创建了RewardedAd对象并添加了一个事件监听器来处理广告请求期间发生的事件。如果广告成功加载,我们设置一个标志表示广告已加载,以便我们可以选择何时显示它。如果在广告请求期间遇到错误,我们将错误存储在state中以便稍后使用。在渲染