-
Notifications
You must be signed in to change notification settings - Fork 122
showRewardedAd
Marc Pascual edited this page Feb 17, 2020
·
11 revisions
title: Show rewarded ad description: Show rewarded ad url: /show-rewarded-ad contributors:
- appfeel
- bitgenoma
- marcpascualsanchez
- miqmago
Show a rewarded ad.
If this method is called before requestRewardedAd, failure callback will be called.
This method must be called when admob.events.onAdLoaded event is raised. If there is no ad available it calls fail callback.
admob.showRewardedAd(success, fail);- success: success callback.
- failure: failure callback.
Note: Rewardeds 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;
function onAdLoaded(e) {
if (isAppForeground) {
if (e.adType === admob.AD_TYPE.REWARDED) {
admob.showRewardedAd();
}
}
}
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);
}
}
}
function onPause() {
if (isAppForeground) {
admob.destroyBannerView();
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);
}
function initAds() {
if (admob) {
var adPublisherIds = {
ios : {
banner : "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",
rewarded: "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII",
},
android : {
banner : "ca-app-pub-XXXXXXXXXXXXXXXX/BBBBBBBBBB",
rewarded: "ca-app-pub-XXXXXXXXXXXXXXXX/IIIIIIIIII",
}
};
var admobid = (/(android)/i.test(navigator.userAgent)) ? adPublisherIds.android : adPublisherIds.ios;
admob.setOptions({
publisherId: admobid.banner,
rewardedId: admobid.rewarded,
autoShowRewarded: false,
});
registerAdEvents();
} else {
alert('AdMobAds plugin not ready');
}
}
function onDeviceReady() {
document.removeEventListener('deviceready', onDeviceReady, false);
initAds();
// request a rewarded ad
admob.requestRewardedAd();
}
document.addEventListener("deviceready", onDeviceReady, false);