[admob][android] Implement RewardedVideo
This commit is contained in:
parent
4852cae988
commit
e6af87209a
|
@ -66,6 +66,11 @@ public class RNFirebaseAdMob extends ReactContextBaseJavaModule {
|
||||||
rewardedVideo.show();
|
rewardedVideo.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param adUnit
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
private RNFirebaseAdmobInterstitial getOrCreateInterstitial(String adUnit) {
|
private RNFirebaseAdmobInterstitial getOrCreateInterstitial(String adUnit) {
|
||||||
if (interstitials.containsKey(adUnit)) {
|
if (interstitials.containsKey(adUnit)) {
|
||||||
return interstitials.get(adUnit);
|
return interstitials.get(adUnit);
|
||||||
|
@ -75,6 +80,11 @@ public class RNFirebaseAdMob extends ReactContextBaseJavaModule {
|
||||||
return interstitial;
|
return interstitial;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param adUnit
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
private RNFirebaseRewardedVideo getOrCreateRewardedVideo(String adUnit) {
|
private RNFirebaseRewardedVideo getOrCreateRewardedVideo(String adUnit) {
|
||||||
if (rewardedVideos.containsKey(adUnit)) {
|
if (rewardedVideos.containsKey(adUnit)) {
|
||||||
return rewardedVideos.get(adUnit);
|
return rewardedVideos.get(adUnit);
|
||||||
|
|
|
@ -99,7 +99,7 @@ class RNFirebaseAdmobInterstitial {
|
||||||
void sendEvent(String type, final @Nullable WritableMap payload) {
|
void sendEvent(String type, final @Nullable WritableMap payload) {
|
||||||
WritableMap map = Arguments.createMap();
|
WritableMap map = Arguments.createMap();
|
||||||
map.putString("type", type);
|
map.putString("type", type);
|
||||||
map.putString("adunit", adUnit);
|
map.putString("adUnit", adUnit);
|
||||||
|
|
||||||
if (payload != null) {
|
if (payload != null) {
|
||||||
map.putMap("payload", payload);
|
map.putMap("payload", payload);
|
||||||
|
|
|
@ -6,9 +6,7 @@ import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import com.facebook.react.bridge.Arguments;
|
import com.facebook.react.bridge.Arguments;
|
||||||
import com.facebook.react.bridge.WritableMap;
|
import com.facebook.react.bridge.WritableMap;
|
||||||
import com.google.android.gms.ads.AdListener;
|
|
||||||
import com.google.android.gms.ads.AdRequest;
|
import com.google.android.gms.ads.AdRequest;
|
||||||
import com.google.android.gms.ads.InterstitialAd;
|
|
||||||
import com.google.android.gms.ads.MobileAds;
|
import com.google.android.gms.ads.MobileAds;
|
||||||
import com.google.android.gms.ads.reward.RewardItem;
|
import com.google.android.gms.ads.reward.RewardItem;
|
||||||
import com.google.android.gms.ads.reward.RewardedVideoAd;
|
import com.google.android.gms.ads.reward.RewardedVideoAd;
|
||||||
|
@ -28,11 +26,23 @@ public class RNFirebaseRewardedVideo implements RewardedVideoAdListener {
|
||||||
adMob = adMobInstance;
|
adMob = adMobInstance;
|
||||||
|
|
||||||
rewardedVideo = MobileAds.getRewardedVideoAdInstance(adMob.getContext());
|
rewardedVideo = MobileAds.getRewardedVideoAdInstance(adMob.getContext());
|
||||||
rewardedVideo.setRewardedVideoAdListener(this);
|
|
||||||
|
Activity activity = adMob.getActivity();
|
||||||
|
final RNFirebaseRewardedVideo _this = this;
|
||||||
|
|
||||||
|
if (activity != null) {
|
||||||
|
activity.runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
rewardedVideo.setRewardedVideoAdListener(_this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load an Ad with a AdRequest instance
|
* Load an Ad with a AdRequest instance
|
||||||
|
*
|
||||||
* @param adRequest
|
* @param adRequest
|
||||||
*/
|
*/
|
||||||
void loadAd(final AdRequest adRequest) {
|
void loadAd(final AdRequest adRequest) {
|
||||||
|
@ -66,33 +76,36 @@ public class RNFirebaseRewardedVideo implements RewardedVideoAdListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRewarded(RewardItem reward) {
|
public void onRewarded(RewardItem reward) {
|
||||||
sendEvent("onRewarded", null);
|
WritableMap payload = Arguments.createMap();
|
||||||
|
payload.putInt("amount", reward.getAmount());
|
||||||
|
payload.putString("type", reward.getType());
|
||||||
|
sendEvent("onRewarded", payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRewardedVideoAdLeftApplication() {
|
public void onRewardedVideoAdLeftApplication() {
|
||||||
sendEvent("onRewardedVideoAdLeftApplication", null);
|
sendEvent("onAdLeftApplication", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRewardedVideoAdClosed() {
|
public void onRewardedVideoAdClosed() {
|
||||||
sendEvent("onRewardedVideoAdClosed", null);
|
sendEvent("onAdClosed", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRewardedVideoAdFailedToLoad(int errorCode) {
|
public void onRewardedVideoAdFailedToLoad(int errorCode) {
|
||||||
WritableMap payload = RNFirebaseAdMobUtils.errorCodeToMap(errorCode);
|
WritableMap payload = RNFirebaseAdMobUtils.errorCodeToMap(errorCode);
|
||||||
sendEvent("onRewardedVideoAdFailedToLoad", payload);
|
sendEvent("onAdFailedToLoad", payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRewardedVideoAdLoaded() {
|
public void onRewardedVideoAdLoaded() {
|
||||||
sendEvent("onRewardedVideoAdLoaded", null);
|
sendEvent("onAdLoaded", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRewardedVideoAdOpened() {
|
public void onRewardedVideoAdOpened() {
|
||||||
sendEvent("onRewardedVideoAdOpened", null);
|
sendEvent("onAdOpened", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -104,13 +117,14 @@ public class RNFirebaseRewardedVideo implements RewardedVideoAdListener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a native event over the bridge with a type and optional payload
|
* Send a native event over the bridge with a type and optional payload
|
||||||
|
*
|
||||||
* @param type
|
* @param type
|
||||||
* @param payload
|
* @param payload
|
||||||
*/
|
*/
|
||||||
void sendEvent(String type, final @Nullable WritableMap payload) {
|
void sendEvent(String type, final @Nullable WritableMap payload) {
|
||||||
WritableMap map = Arguments.createMap();
|
WritableMap map = Arguments.createMap();
|
||||||
map.putString("type", type);
|
map.putString("type", type);
|
||||||
map.putString("adunit", adUnit);
|
map.putString("adUnit", adUnit);
|
||||||
|
|
||||||
if (payload != null) {
|
if (payload != null) {
|
||||||
map.putMap("payload", payload);
|
map.putMap("payload", payload);
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
import { NativeModules } from 'react-native';
|
||||||
|
import { statics } from './';
|
||||||
|
import { nativeToJSError } from '../../utils';
|
||||||
|
|
||||||
|
const FirebaseAdMob = NativeModules.RNFirebaseAdmob;
|
||||||
|
|
||||||
|
export default class RewardedVideo {
|
||||||
|
|
||||||
|
constructor(admob: Object, adunit: string) {
|
||||||
|
this.admob = admob;
|
||||||
|
this.adUnit = adunit;
|
||||||
|
this.loaded = false;
|
||||||
|
this.admob.on(`rewarded_video_${adunit}`, this._onRewardedVideoEvent.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle a JS emit event
|
||||||
|
* @param event
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_onRewardedVideoEvent(event) {
|
||||||
|
const eventType = `rewarded_video:${this.adUnit}:${event.type}`;
|
||||||
|
|
||||||
|
let emitData = Object.assign({}, event);
|
||||||
|
|
||||||
|
switch (event.type) {
|
||||||
|
case 'onAdLoaded':
|
||||||
|
this.loaded = true;
|
||||||
|
break;
|
||||||
|
case 'onAdFailedToLoad':
|
||||||
|
emitData = nativeToJSError(event.payload.code, event.payload.message);
|
||||||
|
emitData.type = event.type;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
this.admob.emit(eventType, emitData);
|
||||||
|
this.admob.emit(`rewarded_video:${this.adUnit}:*`, emitData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load an ad with an instance of AdRequest
|
||||||
|
* @param request
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
loadAd(request: AdRequest) {
|
||||||
|
return FirebaseAdMob.rewardedVideoLoadAd(this.adUnit, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a local instance of isLoaded
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
isLoaded() {
|
||||||
|
return this.loaded;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the advert - will only show if loaded
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
show() {
|
||||||
|
if (this.loaded) {
|
||||||
|
FirebaseAdMob.rewardedVideoShowAd(this.adUnit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen to an Ad event
|
||||||
|
* @param eventType
|
||||||
|
* @param listenerCb
|
||||||
|
* @returns {null}
|
||||||
|
*/
|
||||||
|
on(eventType, listenerCb) {
|
||||||
|
if ((eventType !== 'onRewarded' || eventType !== 'onRewardedVideoStarted') && !statics.EventTypes[eventType]) {
|
||||||
|
console.warn(`Invalid event type provided, must be one of: ${Object.keys(statics.EventTypes).join(', ')}, onRewarded, onRewardedVideoStarted`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.admob.on(`rewarded_video:${this.adUnit}:${eventType}`, listenerCb);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue