React Native Admob Google 移动广告不起作用?

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

我从文档中复制了确切的代码并尝试签入我的应用程序,但它不起作用, 这里我只是删除了按钮,因为我不需要按钮。

广告应在用户访问页面后自动出现。现在,当我运行该应用程序时,广告不会出现。

我需要做什么?我应该把 - interstitial.show(); 放在哪里代码里面

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>
  );
}
react-native admob google-mobile-ads firebase-admob
2个回答
1
投票

您需要对广告有某种触发器,因此如果您希望它们自动加载,您可以在广告加载后执行此操作。在达到这一点之前,您还需要确保已遵循所有安装步骤,包括加载插页式广告之前的

await mobileAds().initialize();

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

0
投票

我认为你应该首先初始化广告(在你的 App.js 中):

import mobileAds from 'react-native-google-mobile-ads';
mobileAds()
.initialize()
.then(() => console.log('Ads initialized'))
© www.soinside.com 2019 - 2024. All rights reserved.