mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-24 04:24:52 +08:00
[admob][android] Start implementation of Rewarded Video
This commit is contained in:
@@ -30,6 +30,7 @@ public class RNFirebaseAdMob extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
private HashMap<String, RNFirebaseAdmobInterstitial> interstitials = new HashMap<>();
|
||||
private HashMap<String, RNFirebaseRewardedVideo> rewardedVideos = new HashMap<>();
|
||||
|
||||
public RNFirebaseAdMob(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
@@ -44,6 +45,53 @@ public class RNFirebaseAdMob extends ReactContextBaseJavaModule {
|
||||
@ReactMethod
|
||||
public void interstitialLoadAd(String adUnit, ReadableMap request) {
|
||||
RNFirebaseAdmobInterstitial interstitial = getOrCreateInterstitial(adUnit);
|
||||
interstitial.loadAd(buildRequest(request).build());
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void interstitialShowAd(String adUnit) {
|
||||
RNFirebaseAdmobInterstitial interstitial = getOrCreateInterstitial(adUnit);
|
||||
interstitial.show();
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void rewardedVideoLoadAd(String adUnit, ReadableMap request) {
|
||||
RNFirebaseRewardedVideo rewardedVideo = getOrCreateRewardedVideo(adUnit);
|
||||
rewardedVideo.loadAd(buildRequest(request).build());
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void rewardedVideoShowAd(String adUnit) {
|
||||
RNFirebaseRewardedVideo rewardedVideo = getOrCreateRewardedVideo(adUnit);
|
||||
rewardedVideo.show();
|
||||
}
|
||||
|
||||
private RNFirebaseAdmobInterstitial getOrCreateInterstitial(String adUnit) {
|
||||
if (interstitials.containsKey(adUnit)) {
|
||||
return interstitials.get(adUnit);
|
||||
}
|
||||
RNFirebaseAdmobInterstitial interstitial = new RNFirebaseAdmobInterstitial(adUnit, this);
|
||||
interstitials.put(adUnit, interstitial);
|
||||
return interstitial;
|
||||
}
|
||||
|
||||
private RNFirebaseRewardedVideo getOrCreateRewardedVideo(String adUnit) {
|
||||
if (rewardedVideos.containsKey(adUnit)) {
|
||||
return rewardedVideos.get(adUnit);
|
||||
}
|
||||
RNFirebaseRewardedVideo rewardedVideo = new RNFirebaseRewardedVideo(adUnit, this);
|
||||
rewardedVideos.put(adUnit, rewardedVideo);
|
||||
return rewardedVideo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getConstants() {
|
||||
final Map<String, Object> constants = new HashMap<>();
|
||||
constants.put("DEVICE_ID_EMULATOR", AdRequest.DEVICE_ID_EMULATOR);
|
||||
return constants;
|
||||
}
|
||||
|
||||
AdRequest.Builder buildRequest(ReadableMap request) {
|
||||
AdRequest.Builder requestBuilder = new AdRequest.Builder();
|
||||
|
||||
if (request.hasKey("testDevice")) {
|
||||
@@ -57,30 +105,6 @@ public class RNFirebaseAdMob extends ReactContextBaseJavaModule {
|
||||
requestBuilder.addKeyword((String) word);
|
||||
}
|
||||
|
||||
interstitial.loadAd(requestBuilder.build());
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void interstitialShowAd(String adUnit) {
|
||||
RNFirebaseAdmobInterstitial interstitial = getOrCreateInterstitial(adUnit);
|
||||
interstitial.show();
|
||||
}
|
||||
|
||||
private RNFirebaseAdmobInterstitial getOrCreateInterstitial(String adUnit) {
|
||||
if (interstitials.containsKey(adUnit)) {
|
||||
return interstitials.get(adUnit);
|
||||
}
|
||||
RNFirebaseAdmobInterstitial interstitial = new RNFirebaseAdmobInterstitial(adUnit, this);
|
||||
interstitials.put(adUnit, interstitial);
|
||||
return interstitial;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getConstants() {
|
||||
final Map<String, Object> constants = new HashMap<>();
|
||||
constants.put("DEVICE_ID_EMULATOR", AdRequest.DEVICE_ID_EMULATOR);
|
||||
return constants;
|
||||
return requestBuilder;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.invertase.firebase.admob;
|
||||
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.google.android.gms.ads.AdRequest;
|
||||
|
||||
class RNFirebaseAdMobUtils {
|
||||
|
||||
/**
|
||||
* Convert common AdMob errors into a standard format
|
||||
* @param errorCode
|
||||
* @return
|
||||
*/
|
||||
static WritableMap errorCodeToMap(int errorCode) {
|
||||
WritableMap map = Arguments.createMap();
|
||||
|
||||
switch (errorCode) {
|
||||
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
|
||||
map.putString("code", "admob/error-code-internal-error");
|
||||
map.putString("message", "Something happened internally; for instance, an invalid response was received from the ad server.");
|
||||
break;
|
||||
case AdRequest.ERROR_CODE_INVALID_REQUEST:
|
||||
map.putString("code", "admob/error-code-invalid-request");
|
||||
map.putString("message", "The ad request was invalid; for instance, the ad unit ID was incorrect.");
|
||||
break;
|
||||
case AdRequest.ERROR_CODE_NETWORK_ERROR:
|
||||
map.putString("code", "admob/error-code-network-error");
|
||||
map.putString("message", "The ad request was unsuccessful due to network connectivity.");
|
||||
break;
|
||||
case AdRequest.ERROR_CODE_NO_FILL:
|
||||
map.putString("code", "admob/error-code-no-fill");
|
||||
map.putString("message", "The ad request was successful, but no ad was returned due to lack of ad inventory.");
|
||||
break;
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package io.invertase.firebase.admob;
|
||||
|
||||
|
||||
import android.app.Activity;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
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.InterstitialAd;
|
||||
import com.google.android.gms.ads.MobileAds;
|
||||
import com.google.android.gms.ads.reward.RewardItem;
|
||||
import com.google.android.gms.ads.reward.RewardedVideoAd;
|
||||
import com.google.android.gms.ads.reward.RewardedVideoAdListener;
|
||||
|
||||
import io.invertase.firebase.Utils;
|
||||
|
||||
public class RNFirebaseRewardedVideo implements RewardedVideoAdListener {
|
||||
|
||||
private RewardedVideoAd mAd;
|
||||
private String adUnit;
|
||||
private RNFirebaseAdMob adMob;
|
||||
private RewardedVideoAd rewardedVideo;
|
||||
|
||||
RNFirebaseRewardedVideo(final String adUnitString, final RNFirebaseAdMob adMobInstance) {
|
||||
adUnit = adUnitString;
|
||||
adMob = adMobInstance;
|
||||
|
||||
rewardedVideo = MobileAds.getRewardedVideoAdInstance(adMob.getContext());
|
||||
rewardedVideo.setRewardedVideoAdListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an Ad with a AdRequest instance
|
||||
* @param adRequest
|
||||
*/
|
||||
void loadAd(final AdRequest adRequest) {
|
||||
Activity activity = adMob.getActivity();
|
||||
if (activity != null) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
rewardedVideo.loadAd(adUnit, adRequest);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the loaded interstitial, if it's loaded
|
||||
*/
|
||||
void show() {
|
||||
Activity activity = adMob.getActivity();
|
||||
if (activity != null) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (rewardedVideo.isLoaded()) {
|
||||
rewardedVideo.show();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewarded(RewardItem reward) {
|
||||
sendEvent("onRewarded", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoAdLeftApplication() {
|
||||
sendEvent("onRewardedVideoAdLeftApplication", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoAdClosed() {
|
||||
sendEvent("onRewardedVideoAdClosed", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoAdFailedToLoad(int errorCode) {
|
||||
WritableMap payload = RNFirebaseAdMobUtils.errorCodeToMap(errorCode);
|
||||
sendEvent("onRewardedVideoAdFailedToLoad", payload);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoAdLoaded() {
|
||||
sendEvent("onRewardedVideoAdLoaded", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoAdOpened() {
|
||||
sendEvent("onRewardedVideoAdOpened", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRewardedVideoStarted() {
|
||||
sendEvent("onRewardedVideoStarted", null);
|
||||
}
|
||||
|
||||
// TODO onResume etc??? https://developers.google.com/admob/android/rewarded-video
|
||||
|
||||
/**
|
||||
* Send a native event over the bridge with a type and optional payload
|
||||
* @param type
|
||||
* @param payload
|
||||
*/
|
||||
void sendEvent(String type, final @Nullable WritableMap payload) {
|
||||
WritableMap map = Arguments.createMap();
|
||||
map.putString("type", type);
|
||||
map.putString("adunit", adUnit);
|
||||
|
||||
if (payload != null) {
|
||||
map.putMap("payload", payload);
|
||||
}
|
||||
|
||||
Utils.sendEvent(adMob.getContext(), "rewarded_video_event", map);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user