Skip to content

Commit

Permalink
Merge pull request #1186 from RodriSanchez1/feature/AdMob
Browse files Browse the repository at this point in the history
Android - Add admob ads
  • Loading branch information
martinbedouret authored May 6, 2022
2 parents 21bf1fe + 865cbca commit 155a661
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/components/Board/Board.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import {
import { NOTIFICATION_DELAY } from '../Notifications/Notifications.constants';
import { EMPTY_VOICES } from '../../providers/SpeechProvider/SpeechProvider.constants';
import { DEFAULT_ROWS_NUMBER, DEFAULT_COLUMNS_NUMBER } from './Board.constants';
//import { isAndroid } from '../../cordova-util';
import { adMobAds, isAndroid } from '../../cordova-util';

const Transition = React.forwardRef(function Transition(props, ref) {
return <Slide direction="up" ref={ref} {...props} />;
Expand Down Expand Up @@ -276,6 +276,9 @@ export class BoardContainer extends Component {
this.setState({ isFixedBoard: !!boardExists.isFixed });

// if (isAndroid()) downloadImages();
if (isAndroid()) {
this.handleAdMobAds();
}
}

UNSAFE_componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -360,6 +363,14 @@ export class BoardContainer extends Component {
}
}

async handleAdMobAds() {
const { bannerAd, interstitialAd } = adMobAds;
if (bannerAd._created) await bannerAd.hide();
await interstitialAd
.load()
.catch(msg => console.error('Error load interstitial Ad', msg));
}

async tryRemoteBoard(boardId) {
const { userData } = this.props;
const remoteBoard = await API.getBoard(boardId);
Expand Down
14 changes: 14 additions & 0 deletions src/components/Settings/Settings.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { logout } from '../Account/Login/Login.actions';
import { getUser, isLogged } from '../App/App.selectors';
import { injectIntl, intlShape } from 'react-intl';
import { disableTour } from '../../components/App/App.actions';
import { adMobAds, isAndroid } from '../../cordova-util';

export class SettingsContainer extends Component {
static propTypes = {
Expand All @@ -16,6 +17,19 @@ export class SettingsContainer extends Component {
logout: PropTypes.func.isRequired,
isDownloadingLang: PropTypes.bool
};
async componentDidMount() {
if (isAndroid()) {
const { bannerAd, interstitialAd } = adMobAds;
interstitialAd
.show()
.catch(msg =>
console.error('The interstitial advice is not available')
);
bannerAd
.show()
.catch(msg => console.error('The banner advice is not available'));
}
}

render() {
return <SettingsComponent {...this.props} />;
Expand Down
7 changes: 6 additions & 1 deletion src/components/WelcomeScreen/WelcomeScreen.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import ResetPassword from '../Account/ResetPassword';
import CboardLogo from './CboardLogo/CboardLogo.component';
import './WelcomeScreen.css';
import { API_URL } from '../../constants';
import { isAndroid, isElectron } from '../../cordova-util';
import { adMobAds, isAndroid, isElectron } from '../../cordova-util';
import { login } from '../Account/Login/Login.actions';

export class WelcomeScreen extends Component {
Expand All @@ -35,6 +35,11 @@ export class WelcomeScreen extends Component {
onClose: PropTypes.func
};

async componentDidMount() {
const { bannerAd } = adMobAds;
if (isAndroid() && bannerAd._created) await bannerAd.hide();
}

handleActiveView = activeView => {
this.setState({
activeView
Expand Down
27 changes: 27 additions & 0 deletions src/cordova-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export const initCordovaPlugins = () => {
} catch (err) {
console.log(err.message);
}
if (isAndroid()) {
try {
prepareAds();
} catch (err) {
console.error(err.message);
}
}
}
};

Expand All @@ -64,6 +71,26 @@ const configFacebookPlugin = () => {
);
};

export const adMobAds = {};

const prepareAds = () => {
const INTERSTITIAL_ADVICE_UNIT_ID =
process.env.REACT_APP_INTERSTITIAL_ADVICE_UNIT_ID ||
'ca-app-pub-3940256099942544/1033173712';

const BANNER_ADVICE_UNIT_ID =
process.env.REACT_APP_BANNER_ADVICE_UNIT_ID ||
'ca-app-pub-3940256099942544/6300978111';

adMobAds.interstitialAd = new window.admob.InterstitialAd({
adUnitId: INTERSTITIAL_ADVICE_UNIT_ID
});

adMobAds.bannerAd = new window.admob.BannerAd({
adUnitId: BANNER_ADVICE_UNIT_ID
});
};

export const cvaTrackEvent = (category, action, label) => {
try {
window.ga.trackEvent(category, action, label);
Expand Down

0 comments on commit 155a661

Please sign in to comment.