Skip to content

Commit

Permalink
feat(android, sdks)!: update to the latest v20 android admob sdk (#32)
Browse files Browse the repository at this point in the history
* Add  onAdClicked()  and Event opened
* Add fullScreenContentCallback for rewarded ad
* Up compile sdk version for example
* Lint fixes
* Move setting test ids to ReactNativeGoogleAdsModule
* Remove smart banner
* Remove AdEvent left application

BREAKING CHANGES:
Please refer to upstream guides for suggestions on new usage. https://developers.google.com/admob/ios/migration and https://developers.google.com/admob/android/migration
- compileSdkVersion now 31, change your app android build.gradle to 31 if you have not already. Note that JDK11 is required for stable compilation on compileSdkVersion 31, JDK8 has internal compiler errors with SDK31
- onAdLeftApplication removed from the underlying SDK, use react-native built in AppState to determine app went to background
- Smart banner ads removed; use adaptive banner ads. Set height/width explicitly taking into account device size
  • Loading branch information
Mikenso authored Dec 12, 2021
1 parent 4fed2d7 commit 291e504
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 152 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ repositories {
}

dependencies {
implementation("com.google.android.gms:play-services-ads:19.8.0") { force = true; }
implementation("com.google.android.gms:play-services-ads:20.5.0") { force = true; }
implementation "com.google.android.ads.consent:consent-library:${ReactNative.ext.getVersion("ads", "consent")}"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*
*/

import androidx.annotation.NonNull;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
Expand All @@ -31,6 +32,7 @@
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.LoadAdError;
import java.util.Map;
import javax.annotation.Nonnull;

Expand All @@ -40,7 +42,6 @@ public class ReactNativeGoogleAdsBannerAdViewManager extends SimpleViewManager<R
private String EVENT_AD_FAILED_TO_LOAD = "onAdFailedToLoad";
private String EVENT_AD_OPENED = "onAdOpened";
private String EVENT_AD_CLOSED = "onAdClosed";
private String EVENT_AD_LEFT_APPLICATION = "onAdLeftApplication";

private Boolean requested = false;
private AdRequest request;
Expand Down Expand Up @@ -97,17 +98,10 @@ public void setRequest(ReactViewGroup reactViewGroup, ReadableMap value) {
public void setSize(ReactViewGroup reactViewGroup, String value) {
size = ReactNativeGoogleAdsCommon.getAdSize(value, reactViewGroup);

int width;
int height;
WritableMap payload = Arguments.createMap();

if (size == AdSize.SMART_BANNER) {
width = (int) PixelUtil.toDIPFromPixel(size.getWidthInPixels(reactViewGroup.getContext()));
height = (int) PixelUtil.toDIPFromPixel(size.getHeightInPixels(reactViewGroup.getContext()));
} else {
width = size.getWidth();
height = size.getHeight();
}
int width = size.getWidth();
int height = size.getHeight();

payload.putDouble("width", width);
payload.putDouble("height", height);
Expand Down Expand Up @@ -160,7 +154,8 @@ public void onAdLoaded() {
}

@Override
public void onAdFailedToLoad(int errorCode) {
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
int errorCode = loadAdError.getCode();
WritableMap payload = ReactNativeGoogleAdsCommon.errorCodeToMap(errorCode);
sendEvent(reactViewGroup, EVENT_AD_FAILED_TO_LOAD, payload);
}
Expand All @@ -174,11 +169,6 @@ public void onAdOpened() {
public void onAdClosed() {
sendEvent(reactViewGroup, EVENT_AD_CLOSED, null);
}

@Override
public void onAdLeftApplication() {
sendEvent(reactViewGroup, EVENT_AD_LEFT_APPLICATION, null);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ static AdSize stringToAdSize(String value) {
return AdSize.FULL_BANNER;
case "LEADERBOARD":
return AdSize.LEADERBOARD;
case "SMART_BANNER":
return AdSize.SMART_BANNER;
default:
case "BANNER":
return AdSize.BANNER;
Expand Down Expand Up @@ -161,21 +159,6 @@ public static AdRequest buildAdRequest(ReadableMap adRequestOptions) {
}
}

if (adRequestOptions.hasKey("testDevices")) {
ArrayList<Object> devices =
Objects.requireNonNull(adRequestOptions.getArray("testDevices")).toArrayList();

for (Object device : devices) {
String id = (String) device;

if (id.equals("EMULATOR")) {
builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
} else {
builder.addTestDevice(id);
}
}
}

if (adRequestOptions.hasKey("contentUrl")) {
builder.setContentUrl(Objects.requireNonNull(adRequestOptions.getString("contentUrl")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class ReactNativeGoogleAdsEvent implements NativeEvent {
public static final String GOOGLE_ADS_EVENT_ERROR = "error";
public static final String GOOGLE_ADS_EVENT_OPENED = "opened";
public static final String GOOGLE_ADS_EVENT_CLICKED = "clicked";
public static final String GOOGLE_ADS_EVENT_LEFT_APPLICATION = "left_application";
public static final String GOOGLE_ADS_EVENT_CLOSED = "closed";

public static final String GOOGLE_ADS_EVENT_REWARDED_LOADED = "rewarded_loaded";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@
*
*/

import static io.invertase.googleads.ReactNativeGoogleAdsCommon.buildAdRequest;
import static io.invertase.googleads.ReactNativeGoogleAdsCommon.getCodeAndMessageFromAdErrorCode;
import static io.invertase.googleads.ReactNativeGoogleAdsCommon.sendAdEvent;
import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_CLICKED;
import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_CLOSED;
import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_ERROR;
import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_LEFT_APPLICATION;
import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_INTERSTITIAL;
import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_LOADED;
import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_OPENED;

import android.app.Activity;
import android.util.SparseArray;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import io.invertase.googleads.common.ReactNativeModule;
import javax.annotation.Nullable;

Expand All @@ -50,8 +53,7 @@ public ReactNativeGoogleAdsInterstitialModule(ReactApplicationContext reactConte

private void sendInterstitialEvent(
String type, int requestId, String adUnitId, @Nullable WritableMap error) {
sendAdEvent(
ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_INTERSTITIAL, requestId, type, adUnitId, error);
sendAdEvent(GOOGLE_ADS_EVENT_INTERSTITIAL, requestId, type, adUnitId, error);
}

@ReactMethod
Expand All @@ -67,51 +69,51 @@ public void interstitialLoad(int requestId, String adUnitId, ReadableMap adReque
}
currentActivity.runOnUiThread(
() -> {
InterstitialAd interstitialAd = new InterstitialAd(currentActivity);
interstitialAd.setAdUnitId(adUnitId);
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
AdRequest adRequest = adRequestBuilder.build();

// Apply AdRequest builder
interstitialAd.loadAd(buildAdRequest(adRequestOptions));
InterstitialAdLoadCallback interstitialAdLoadCallback =
new InterstitialAdLoadCallback() {

interstitialAd.setAdListener(
new AdListener() {
@Override
public void onAdLoaded() {
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {

interstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
sendInterstitialEvent(GOOGLE_ADS_EVENT_CLOSED, requestId, adUnitId, null);
interstitialAdArray.put(requestId, null);
}

@Override
public void onAdClicked() {
sendInterstitialEvent(
GOOGLE_ADS_EVENT_CLICKED, requestId, adUnitId, null);
}

@Override
public void onAdShowedFullScreenContent() {
sendInterstitialEvent(GOOGLE_ADS_EVENT_OPENED, requestId, adUnitId, null);
}
});

interstitialAdArray.put(requestId, interstitialAd);
sendInterstitialEvent(GOOGLE_ADS_EVENT_LOADED, requestId, adUnitId, null);
}

@Override
public void onAdFailedToLoad(int errorCode) {
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
WritableMap error = Arguments.createMap();
int errorCode = loadAdError.getCode();
String[] codeAndMessage = getCodeAndMessageFromAdErrorCode(errorCode);
error.putString("code", codeAndMessage[0]);
error.putString("message", codeAndMessage[1]);
sendInterstitialEvent(GOOGLE_ADS_EVENT_ERROR, requestId, adUnitId, error);
}
};

@Override
public void onAdOpened() {
sendInterstitialEvent(GOOGLE_ADS_EVENT_OPENED, requestId, adUnitId, null);
}

@Override
public void onAdClicked() {
sendInterstitialEvent(GOOGLE_ADS_EVENT_CLICKED, requestId, adUnitId, null);
}

@Override
public void onAdLeftApplication() {
sendInterstitialEvent(
GOOGLE_ADS_EVENT_LEFT_APPLICATION, requestId, adUnitId, null);
}

@Override
public void onAdClosed() {
sendInterstitialEvent(GOOGLE_ADS_EVENT_CLOSED, requestId, adUnitId, null);
}
});

interstitialAdArray.put(requestId, interstitialAd);
InterstitialAd.load(currentActivity, adUnitId, adRequest, interstitialAdLoadCallback);
});
}

Expand Down Expand Up @@ -142,8 +144,10 @@ public void interstitialShow(int requestId, ReadableMap showOptions, Promise pro
interstitialAd.setImmersiveMode(false);
}

if (interstitialAd.isLoaded()) {
interstitialAd.show();
String a = String.valueOf(requestId);

if (interstitialAd != null) {
interstitialAd.show(getCurrentActivity());
promise.resolve(null);
} else {
rejectPromiseWithCodeAndMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.RequestConfiguration;
import io.invertase.googleads.common.ReactNativeModule;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ReactNativeGoogleAdsModule extends ReactNativeModule {
Expand All @@ -36,6 +39,25 @@ public class ReactNativeGoogleAdsModule extends ReactNativeModule {
private RequestConfiguration buildRequestConfiguration(ReadableMap requestConfiguration) {
RequestConfiguration.Builder builder = new RequestConfiguration.Builder();

if (requestConfiguration.hasKey("testDeviceIdentifiers")) {
ArrayList<Object> devices =
Objects.requireNonNull(requestConfiguration.getArray("testDeviceIdentifiers"))
.toArrayList();

List<String> testDeviceIds = new ArrayList<>();

for (Object device : devices) {
String id = (String) device;

if (id.equals("EMULATOR")) {
testDeviceIds.add(AdRequest.DEVICE_ID_EMULATOR);
} else {
testDeviceIds.add(id);
}
}
builder.setTestDeviceIds(testDeviceIds);
}

if (requestConfiguration.hasKey("maxAdContentRating")) {
String rating = requestConfiguration.getString("maxAdContentRating");

Expand Down
Loading

0 comments on commit 291e504

Please sign in to comment.