简体   繁体   English

Admob 奖励广告在 Expo App 上的 ios 上不起作用

[英]Admob reward ads don't work on ios on Expo App

On App.js I have initialized AdMobRewarded as following:在 App.js 上,我已将 AdMobRewarded 初始化如下:

if (Platform.OS === 'ios') {
  AdMobRewarded.setAdUnitID('ca-app-pub-xxx/xxx');
} else {
  AdMobRewarded.setAdUnitID('ca-app-pub-xxx/xxx');
}

And here is the class:这是课程:

export default class App extends React.Component {
  state = {
    fontsLoaded: false,
  };

  render() {
    const { fontsLoaded } = this.state;
    if (!fontsLoaded) {
      return (
        <AppLoading
          startAsync={fetchFonts}
          onFinish={() => this.setState({ fontsLoaded: true })}
        />
      );
    }
    return (
      <Provider store={store}>
        <AppContainer
          ref={navigatorRef => {
            NavigationService.setTopLevelNavigator(navigatorRef);
          }}
        />
        <CommonComponents />
      </Provider>
    );
  }
}

Inside the CommonComponents I have put the listener for AdMobRewarded:在 CommonComponents 中,我放置了 AdMobRewarded 的监听器:

useEffect(() => {
  AdMobRewarded.addEventListener('rewardedVideoDidRewardUser', () => {
    setState({
      hintModalVisible: true,
      adIsLoading: false,
      mainMenuVisible: false,
    });
  });

  return () => {
    AdMobRewarded.removeAllListeners();
  };
}, []);

setState is actually not React setState, it's a redux action I have implemented: setState 实际上不是 React setState,它是我实现的 redux 操作:

const setStateAction = (obj, sceneName) => {
  const type = sceneName ? `${sceneName}_SET_STATE` : 'SET_STATE';
  return { ...obj, type };
};

Without the rewardedVideoDidRewardUser listener, calling setState does open the Modal and everything is fine.如果没有rewardedVideoDidRewardUser监听器,调用setState会打开 Modal,一切都很好。

hintModalVisible is used for Modal isVisible prop, which opens and closes the Modal. hintModalVisible用于 Modal isVisible道具,它打开和关闭 Modal。

On Android everything works as expected, but there is a strange behavior on iOS.在 Android 上一切正常,但在 iOS 上有一个奇怪的行为。 The ad shows for a second and automatically closes, and the Hint Modal doesn't open.广告显示一秒钟并自动关闭,提示模式未打开。

Here is the function that requests and shows the ad.这是请求和显示广告的函数。 It is present in all screens of the app:它存在于应用程序的所有屏幕中:

showHint = async () => {
  const { setState } = this.props;
  try {
    setState({
      mainMenuVisible: false,
    });
    await AdMobRewarded.requestAdAsync();
    await AdMobRewarded.showAdAsync();
  } catch (e) {
    setState({
      hintModalVisible: true,
      mainMenuVisible: false,
    });
  }
};

It's an open source project, so you can the code here这是一个开源项目,所以你可以在这里查看代码

The problem was with React Native Modal.问题出在 React Native Modal 上。

setState({
  hintModalVisible: true,
  adIsLoading: false,
  mainMenuVisible: false,
});

This block of code should have closed the main menu modal and open the hint modal.这段代码应该关闭了主菜单模式并打开了提示模式。 But it seems that on IOS you cannot do this simultaneously.但似乎在 IOS 上您不能同时执行此操作。 So this is how I handled it.所以我就是这样处理的。

useEffect(() => {
  AdMobRewarded.addEventListener('rewardedVideoDidRewardUser', () => {
    setTimeout(() => {
      setState({
        hintModalVisible: true,
      });
    }, 1000);
  });

  return () => {
    AdMobRewarded.removeAllListeners();
  };
}, []);

And took the closing of main menu modal in the ad requesting function:并在广告请求功能中关闭主菜单模式:

const requestHint = useCallback(async () => {
  try {
    setState({
      mainMenuVisible: false,
    });
    await AdMobRewarded.requestAdAsync();
    await AdMobRewarded.showAdAsync();
  } catch (e) {
    setState({
      mainMenuVisible: false,
    });
    setTimeout(() => {
      setState({
        hintModalVisible: true,
      });
    }, 500);
  }
}, [setState, hintModalVisible]);

So this is not concerned to the Admob Rewarded.所以这与 Admob Rewarded 无关。 It is more a React Native Modal bug.它更像是一个 React Native Modal 错误。 https://github.com/react-native-community/react-native-modal/issues/192 https://github.com/react-native-community/react-native-modal/issues/192

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM