-
Notifications
You must be signed in to change notification settings - Fork 127
showRewardedAd
Marc Pascual edited this page Jun 16, 2020
·
11 revisions
Show a rewarded ad.
If this method is called before requestRewardedAd, an exception will be thrown.
This method must be called when admob.events.onAdLoaded
event is raised. If there is no ad available an exception will be thrown.
try {
await admob.showRewardedAd();
} catch (err) {
console.log('Error showing/hiding rewarded ad:', err);
}
Note: Rewarded ads may be shown even if the android app is in background. For this reason it is recommended to stop rewarded ads and reload them when onPause
and onResume
events are detected.
This is an example on how to show rewarded ads every 2 minutes preventing to show them when the app is in background:
var isAppForeground = true;
async function onAdLoaded(e) {
if (isAppForeground) {
if (e.adType === admob.AD_TYPE.REWARDED) {
try {
await admob.showRewardedAd();
} catch (err) {
console.log('Error showing/hiding rewarded ad:', err);
}
}
}
}
function onRewardedAd(e) {
if (isAppForeground) {
if (e.adType === admob.AD_TYPE.REWARDED) {
setTimeout(admob.requestRewardedAd, 1000 * 60 * 2);
console.log('The user has been rewarded! Please unlock feature in your app, give present, live, coins, whatever you promised him/her');
}
}
}
function onRewardedAdVideoStarted(e) {
if (isAppForeground) {
if (e.adType === admob.AD_TYPE.REWARDED) {
console.log('Rewarded video just started now');
}
}
}
function onRewardedAdVideoCompleted(e) {
if (isAppForeground) {
if (e.adType === admob.AD_TYPE.REWARDED) {
console.log('Rewarded video just finished now');
}
}
}
function onAdClosed(e) {
if (isAppForeground) {
if (e.adType === admob.AD_TYPE.REWARDED) {
setTimeout(admob.requestRewardedAd, 1000 * 60 * 2);
}
}
}
async function onPause() {
if (isAppForeground) {
try {
await admob.destroyBannerView();
} catch (err) {
console.log('Error destroying banner:', err);
}
isAppForeground = false;
}
}
function onResume() {
if (!isAppForeground) {
setTimeout(admob.requestRewardedAd, 1);
isAppForeground = true;
}
}
// optional, in case respond to events
function registerAdEvents() {
document.addEventListener(admob.events.onAdLoaded, onAdLoaded);
document.addEventListener(admob.events.onRewardedAd, onRewardedAd);
document.addEventListener(admob.events.onRewardedAdVideoStarted, onRewardedAdVideoStarted);
document.addEventListener(admob.events.onRewardedAdVideoCompleted, onRewardedAdVideoCompleted);
document.addEventListener(admob.events.onAdClosed, onAdClosed);
document.addEventListener('pause', onPause, false);
document.addEventListener('resume', onResume, false);
}
async function initAds() {
if (admob) {
var adIds = {
ios : {
bannerAdId: 'ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB',
rewarded: 'ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII',
},
android : {
bannerAdId: 'ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB',
rewarded: 'ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII',
}
};
var admobid = (/(android)/i.test(navigator.userAgent)) ? adIds.android : adIds.ios;
try {
await admob.setOptions({
bannerAdId: admobid.bannerAdId,
rewardedId: admobid.rewarded,
autoShowRewarded: false,
});
} catch (err) {
console.log('Error setting options:', err);
}
registerAdEvents();
} else {
alert('AdMobAds plugin not ready');
}
}
async function onDeviceReady() {
document.removeEventListener('deviceready', onDeviceReady, false);
await initAds();
// request a rewarded ad
try {
await admob.requestRewardedAd();
} catch (err) {
console.log('Error destroying banner:', err);
}
}
document.addEventListener('deviceready', onDeviceReady, false);