我已使用 Expo 和
react-native-google-mobile-ads
将 AdMob 集成到我的 React Native 应用程序中。在本地,使用测试 AdUnit ID (ca-app-pub-3940256099942544/6978759866
),模拟器中一切正常,我可以毫无问题地看到广告。但是,当我切换到 AdMob 提供的生产 AdUnit ID (ca-app-pub-xxx/xxx
) 并使用 Expo 构建应用程序时,我无法在 Expo Go 应用程序或 TestFlight 中看到广告。
这是我的设置: app.json 配置:
{
"expo": {
...
"plugins": [
[
"react-native-google-mobile-ads",
{
"androidAppId": "ca-app-pub-xxx~xxx",
"iosAppId": "ca-app-pub-xxx~xxx"
}
]
],
"ios": {
...
"infoPlist": {
"SKAdNetworkItems": [
{
"SKAdNetworkIdentifier": [
"cstr6suwn9.skadnetwork",
"4fzdc2evr5.skadnetwork",
...
]
}
]
}
}
}
}
AdMob 组件代码:
import { RewardedInterstitialAd, RewardedAdEventType, TestIds, AdEventType } from 'react-native-google-mobile-ads';
import React, { useEffect, useState } from 'react';
import { View, StyleSheet } from 'react-native';
const adUnitId = 'ca-app-pub-xxx/xxx'; // Production Ad Unit ID
const rewardedInterstitial = RewardedInterstitialAd.createForAdRequest(adUnitId, {
requestNonPersonalizedAdsOnly: true,
keywords: ['fashion', 'clothing'],
});
const RewardedInterstitialAdComponent = () => {
const [loaded, setLoaded] = useState(false);
useEffect(() => {
const unsubscribeLoaded = rewardedInterstitial.addAdEventListener(RewardedAdEventType.LOADED, () => {
console.log('Ad loaded');
setLoaded(true);
});
const unsubscribeEarned = rewardedInterstitial.addAdEventListener(RewardedAdEventType.EARNED_REWARD, (reward) => {
console.log('User earned reward', reward);
});
const unsubscribeClosed = rewardedInterstitial.addAdEventListener(AdEventType.CLOSED, () => {
rewardedInterstitial.load();
});
rewardedInterstitial.load();
return () => {
unsubscribeLoaded();
unsubscribeEarned();
unsubscribeClosed();
};
}, []);
if (!loaded) {
return null;
}
rewardedInterstitial.show();
return <View style={styles.container} />;
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
export default RewardedInterstitialAdComponent;
问题:
问题:
这是开发模式所期望的。您可以使用
__DEV__
来定义不同的 ID。
export default {
BANNER: __DEV__ ? TestIds.BANNER : BANNER_ID,
INTERSTITIAL: __DEV__ ? TestIds.INTERSTITIAL : INTERSTITIAL_ID,
};
对于新应用程序,根据文档显示广告可能需要一些时间:
新的 iOS 应用程序在 Apple App Store 中列出之前不会显示 Google 广告