AnswerBun.com

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

Stack Overflow Asked by Mikayel Saghyan on December 31, 2020

1 Answers

One Answer

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

Correct answer by Mikayel Saghyan on December 31, 2020

Add your own answers!

Related Questions

Center a grouped bar chart in R (ggplot2)

2  Asked on December 20, 2021

   

Node js – public subfolder

1  Asked on December 20, 2021 by andres-darwin

 

Syntax error on getting multiple range VBA

0  Asked on December 20, 2021 by michael-norman

     

what can make my form not to see my input value

2  Asked on December 18, 2021 by michael-august

     

Regex Match/Wildcard

1  Asked on December 18, 2021 by ovenbakedpython

     

javascript async and await event listener: Pokemon

1  Asked on December 18, 2021 by jason-todd

   

Why do I want to use CRTP over simple template for static polymorphism?

2  Asked on December 18, 2021 by diveintoml

   

Check if string in path and do something

2  Asked on December 18, 2021

 

sql, delete duplicate records using an ID

2  Asked on December 18, 2021 by user12929912

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.