Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Admob reward ads don't work on ios on Expo App

On App.js I have initialized AdMobRewarded as following:

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:

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:

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.

hintModalVisible is used for Modal isVisible prop, which opens and closes the Modal.

On Android everything works as expected, but there is a strange behavior on 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

like image 220
Mikayel Saghyan Avatar asked Nov 24 '25 18:11

Mikayel Saghyan


1 Answers

The problem was with 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. 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. It is more a React Native Modal bug. https://github.com/react-native-community/react-native-modal/issues/192

like image 160
Mikayel Saghyan Avatar answered Nov 26 '25 07:11

Mikayel Saghyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!