From 254e763c601cbfd29b32dd930188f77d3021ca50 Mon Sep 17 00:00:00 2001 From: Dylan Companjen Date: Tue, 28 Dec 2021 23:12:46 +0100 Subject: [PATCH 1/8] docs: use correct TestId for RewardedAd --- docs/displaying-ads.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/displaying-ads.mdx b/docs/displaying-ads.mdx index 788d3ed6..0b8483f4 100644 --- a/docs/displaying-ads.mdx +++ b/docs/displaying-ads.mdx @@ -107,7 +107,7 @@ Google AdMob dashboard under "Ad units" should be used: ```js import { RewardedAd, TestIds } from '@invertase/react-native-google-ads'; -const adUnitId = __DEV__ ? TestIds.INTERSTITIAL : 'ca-app-pub-xxxxxxxxxxxxx/yyyyyyyyyyyyyy'; +const adUnitId = __DEV__ ? TestIds.REWARDED : 'ca-app-pub-xxxxxxxxxxxxx/yyyyyyyyyyyyyy'; const rewarded = RewardedAd.createForAdRequest(adUnitId, { requestNonPersonalizedAdsOnly: true, From de3d632369bf9f74881f48697345e433c96959d4 Mon Sep 17 00:00:00 2001 From: Dylan Companjen Date: Wed, 29 Dec 2021 14:50:09 +0100 Subject: [PATCH 2/8] refactor(ios): use the available error msg instead of a custom msg --- ios/RNGoogleAds/RNGoogleAdsCommon.m | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/ios/RNGoogleAds/RNGoogleAdsCommon.m b/ios/RNGoogleAds/RNGoogleAdsCommon.m index 0fa0e89d..979a8201 100644 --- a/ios/RNGoogleAds/RNGoogleAdsCommon.m +++ b/ios/RNGoogleAds/RNGoogleAdsCommon.m @@ -88,54 +88,40 @@ + (GADRequest *)buildAdRequest:(NSDictionary *)adRequestOptions { + (NSDictionary *)getCodeAndMessageFromAdError:(NSError *)error { NSString *code = @"unknown"; NSString *message = @"An unknown error occurred."; + if ([error localizedDescription] != nil) { + message = [error localizedDescription]; + } if (error.code == GADErrorInvalidRequest) { code = @"invalid-request"; - message = @"The ad request was invalid; for instance, the ad unit ID was incorrect."; } else if (error.code == GADErrorNoFill) { code = @"no-fill"; - message = @"The ad request was successful, but no ad was returned due to lack of ad inventory."; } else if (error.code == GADErrorNetworkError) { code = @"network-error"; - message = @"The ad request was unsuccessful due to network connectivity."; } else if (error.code == GADErrorServerError) { code = @"server-error"; - message = @"The ad server experienced a failure processing the request."; } else if (error.code == GADErrorOSVersionTooLow) { code = @"os-version-too-low"; - message = @"The current device’s OS is below the minimum required version."; } else if (error.code == GADErrorTimeout) { code = @"timeout"; - message = @"The request was unable to be loaded before being timed out."; } else if (error.code == GADErrorMediationDataError) { code = @"mediation-data-error"; - message = @"The mediation response was invalid."; } else if (error.code == GADErrorMediationAdapterError) { code = @"mediation-adapter-error"; - message = @"Error finding or creating a mediation ad network adapter."; } else if (error.code == GADErrorMediationInvalidAdSize) { code = @"mediation-invalid-ad-size"; - message = @"Attempting to pass an invalid ad size to an adapter."; } else if (error.code == GADErrorInternalError) { code = @"internal-error"; - message = @"Something happened internally; for instance, an invalid response was received from " - @"the ad server."; } else if (error.code == GADErrorInvalidArgument) { code = @"invalid-argument"; - message = @"Invalid argument error."; } else if (error.code == GADErrorReceivedInvalidResponse) { code = @"received-invalid-response"; - message = @"Received invalid response."; } else if (error.code == GADErrorMediationNoFill) { code = @"mediation-no-fill"; - message = @"A mediation ad network adapter received an ad request, but did not fill. The " - @"adapter’s error is included as an underlyingError."; } else if (error.code == GADErrorAdAlreadyUsed) { code = @"ad-already-used"; - message = @"Will not send request because the ad object has already been used."; } else if (error.code == GADErrorApplicationIdentifierMissing) { code = @"application-identifier-missing"; - message = @"Will not send request because the application identifier is missing."; } return @{ From b4bd085c390b46258c1e2d2e6cd492bcb3c89018 Mon Sep 17 00:00:00 2001 From: Dylan Companjen Date: Wed, 29 Dec 2021 15:01:22 +0100 Subject: [PATCH 3/8] refactor(android): use the available error msg instead of a custom msg --- .../googleads/ReactNativeGoogleAdsCommon.java | 21 ++----------------- ...eactNativeGoogleAdsInterstitialModule.java | 8 +++---- .../ReactNativeGoogleAdsRewardedModule.java | 8 +++---- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java index efb7f891..b0989d31 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java @@ -216,53 +216,36 @@ public static void sendAdEvent( emitter.sendEvent(new ReactNativeGoogleAdsEvent(event, requestId, adUnitId, eventBody)); } - public static String[] getCodeAndMessageFromAdErrorCode(int errorCode) { + public static String[] getCodeFromAdErrorCode(int errorCode) { String code = "unknown"; - String message = "An unknown error occurred."; switch (errorCode) { case AdRequest.ERROR_CODE_APP_ID_MISSING: code = "app-id-missing"; - message = "The ad request was not made due to a missing app ID."; break; case AdRequest.ERROR_CODE_INTERNAL_ERROR: code = "internal-error"; - message = - "Something happened internally; for instance, an invalid response was received from the" - + " ad server."; break; case AdRequest.ERROR_CODE_INVALID_AD_STRING: code = "invalid-ad-string"; - message = "The ad string is invalid."; break; case AdRequest.ERROR_CODE_INVALID_REQUEST: code = "invalid-request"; - message = "The ad request was invalid; for instance, the ad unit ID was incorrect."; break; case AdRequest.ERROR_CODE_MEDIATION_NO_FILL: code = "mediation-no-fill"; - message = "The mediation adapter did not fill the ad request."; break; case AdRequest.ERROR_CODE_NETWORK_ERROR: code = "network-error"; - message = "The ad request was unsuccessful due to network connectivity."; break; case AdRequest.ERROR_CODE_NO_FILL: code = "no-fill"; - message = - "The ad request was successful, but no ad was returned due to lack of ad inventory."; break; case AdRequest.ERROR_CODE_REQUEST_ID_MISMATCH: code = "request-id-mismatch"; - message = - "The AdInfo object inside the ad request has mismatching request IDs or the request ID" - + " in the ad string is not found."; break; } - String[] codeAndMessage = new String[2]; - codeAndMessage[0] = code; - codeAndMessage[1] = message; - return codeAndMessage; + return code; } } diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java index de934e25..7da12f95 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java @@ -17,7 +17,7 @@ * */ -import static io.invertase.googleads.ReactNativeGoogleAdsCommon.getCodeAndMessageFromAdErrorCode; +import static io.invertase.googleads.ReactNativeGoogleAdsCommon.getCodeFromAdErrorCode; 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; @@ -106,9 +106,9 @@ public void onAdShowedFullScreenContent() { 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]); + String code = getCodeFromAdErrorCode(errorCode); + error.putString("code", code); + error.putString("message", loadAdError.getMessage()); sendInterstitialEvent(GOOGLE_ADS_EVENT_ERROR, requestId, adUnitId, error); } }; diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java index 2e9d1a12..605063bd 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java @@ -1,6 +1,6 @@ package io.invertase.googleads; -import static io.invertase.googleads.ReactNativeGoogleAdsCommon.getCodeAndMessageFromAdErrorCode; +import static io.invertase.googleads.ReactNativeGoogleAdsCommon.getCodeFromAdErrorCode; import static io.invertase.googleads.ReactNativeGoogleAdsCommon.sendAdEvent; import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_CLOSED; import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_ERROR; @@ -72,9 +72,9 @@ public void rewardedLoad(int requestId, String adUnitId, ReadableMap adRequestOp 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]); + String code = getCodeFromAdErrorCode(errorCode); + error.putString("code", code); + error.putString("message", loadAdError.getMessage()); sendRewardedEvent(GOOGLE_ADS_EVENT_ERROR, requestId, adUnitId, error, null); } From 6dd81575ee9e552f8f9c20fd64949df9fc8af044 Mon Sep 17 00:00:00 2001 From: Dylan Companjen Date: Wed, 29 Dec 2021 15:11:09 +0100 Subject: [PATCH 4/8] fix(android): method return type --- .../java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java index b0989d31..7eed7da1 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java @@ -216,7 +216,7 @@ public static void sendAdEvent( emitter.sendEvent(new ReactNativeGoogleAdsEvent(event, requestId, adUnitId, eventBody)); } - public static String[] getCodeFromAdErrorCode(int errorCode) { + public static String getCodeFromAdErrorCode(int errorCode) { String code = "unknown"; switch (errorCode) { From a6a6ef267fc606866829b64502a5828c93b414f1 Mon Sep 17 00:00:00 2001 From: Dylan Companjen Date: Wed, 29 Dec 2021 15:15:30 +0100 Subject: [PATCH 5/8] refactor(ios): remove redundant unknown message --- ios/RNGoogleAds/RNGoogleAdsCommon.m | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ios/RNGoogleAds/RNGoogleAdsCommon.m b/ios/RNGoogleAds/RNGoogleAdsCommon.m index 979a8201..792886a1 100644 --- a/ios/RNGoogleAds/RNGoogleAdsCommon.m +++ b/ios/RNGoogleAds/RNGoogleAdsCommon.m @@ -87,10 +87,7 @@ + (GADRequest *)buildAdRequest:(NSDictionary *)adRequestOptions { + (NSDictionary *)getCodeAndMessageFromAdError:(NSError *)error { NSString *code = @"unknown"; - NSString *message = @"An unknown error occurred."; - if ([error localizedDescription] != nil) { - message = [error localizedDescription]; - } + NSString *message = [error localizedDescription]; if (error.code == GADErrorInvalidRequest) { code = @"invalid-request"; From b8aaceabc1de6186db2d9ada8b0aee628b981d74 Mon Sep 17 00:00:00 2001 From: Dylan Companjen Date: Wed, 29 Dec 2021 15:34:26 +0100 Subject: [PATCH 6/8] line up with ios --- .../googleads/ReactNativeGoogleAdsCommon.java | 11 ++++++++--- .../ReactNativeGoogleAdsInterstitialModule.java | 9 ++++----- .../googleads/ReactNativeGoogleAdsRewardedModule.java | 9 ++++----- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java index 7eed7da1..9a621833 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java @@ -30,6 +30,7 @@ import com.google.ads.mediation.admob.AdMobAdapter; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdSize; +import com.google.android.gms.ads.AdError; import io.invertase.googleads.common.ReactNativeEventEmitter; import java.util.ArrayList; import java.util.Map; @@ -216,10 +217,11 @@ public static void sendAdEvent( emitter.sendEvent(new ReactNativeGoogleAdsEvent(event, requestId, adUnitId, eventBody)); } - public static String getCodeFromAdErrorCode(int errorCode) { + public static String[] getCodeAndMessageFromAdError(AdError adError) { String code = "unknown"; + String message = adError.getMessage(); - switch (errorCode) { + switch (adError.getCode()) { case AdRequest.ERROR_CODE_APP_ID_MISSING: code = "app-id-missing"; break; @@ -246,6 +248,9 @@ public static String getCodeFromAdErrorCode(int errorCode) { break; } - return code; + String[] codeAndMessage = new String[2]; + codeAndMessage[0] = code; + codeAndMessage[1] = message; + return codeAndMessage; } } diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java index 7da12f95..3d8b8be4 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java @@ -17,7 +17,7 @@ * */ -import static io.invertase.googleads.ReactNativeGoogleAdsCommon.getCodeFromAdErrorCode; +import static io.invertase.googleads.ReactNativeGoogleAdsCommon.getCodeAndMessageFromAdError; 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; @@ -105,10 +105,9 @@ public void onAdShowedFullScreenContent() { @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { WritableMap error = Arguments.createMap(); - int errorCode = loadAdError.getCode(); - String code = getCodeFromAdErrorCode(errorCode); - error.putString("code", code); - error.putString("message", loadAdError.getMessage()); + String[] codeAndMessage = getCodeAndMessageFromAdErrorCode(loadAdError); + error.putString("code", codeAndMessage[0]); + error.putString("message", codeAndMessage[1]); sendInterstitialEvent(GOOGLE_ADS_EVENT_ERROR, requestId, adUnitId, error); } }; diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java index 605063bd..5437c75e 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java @@ -1,6 +1,6 @@ package io.invertase.googleads; -import static io.invertase.googleads.ReactNativeGoogleAdsCommon.getCodeFromAdErrorCode; +import static io.invertase.googleads.ReactNativeGoogleAdsCommon.getCodeAndMessageFromAdErrorCode; import static io.invertase.googleads.ReactNativeGoogleAdsCommon.sendAdEvent; import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_CLOSED; import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_ERROR; @@ -71,10 +71,9 @@ public void rewardedLoad(int requestId, String adUnitId, ReadableMap adRequestOp @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { WritableMap error = Arguments.createMap(); - int errorCode = loadAdError.getCode(); - String code = getCodeFromAdErrorCode(errorCode); - error.putString("code", code); - error.putString("message", loadAdError.getMessage()); + String[] codeAndMessage = getCodeAndMessageFromAdErrorCode(loadAdError); + error.putString("code", codeAndMessage[0]); + error.putString("message", codeAndMessage[1]); sendRewardedEvent(GOOGLE_ADS_EVENT_ERROR, requestId, adUnitId, error, null); } From d4a8cc0a661b6e30b194b54c0c346218dc2b8dcc Mon Sep 17 00:00:00 2001 From: Dylan Companjen Date: Wed, 29 Dec 2021 15:36:39 +0100 Subject: [PATCH 7/8] fix import --- .../googleads/ReactNativeGoogleAdsInterstitialModule.java | 2 +- .../googleads/ReactNativeGoogleAdsRewardedModule.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java index 3d8b8be4..284e064d 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsInterstitialModule.java @@ -105,7 +105,7 @@ public void onAdShowedFullScreenContent() { @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { WritableMap error = Arguments.createMap(); - String[] codeAndMessage = getCodeAndMessageFromAdErrorCode(loadAdError); + String[] codeAndMessage = getCodeAndMessageFromAdError(loadAdError); error.putString("code", codeAndMessage[0]); error.putString("message", codeAndMessage[1]); sendInterstitialEvent(GOOGLE_ADS_EVENT_ERROR, requestId, adUnitId, error); diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java index 5437c75e..53fa7094 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsRewardedModule.java @@ -1,6 +1,6 @@ package io.invertase.googleads; -import static io.invertase.googleads.ReactNativeGoogleAdsCommon.getCodeAndMessageFromAdErrorCode; +import static io.invertase.googleads.ReactNativeGoogleAdsCommon.getCodeAndMessageFromAdError; import static io.invertase.googleads.ReactNativeGoogleAdsCommon.sendAdEvent; import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_CLOSED; import static io.invertase.googleads.ReactNativeGoogleAdsEvent.GOOGLE_ADS_EVENT_ERROR; @@ -71,7 +71,7 @@ public void rewardedLoad(int requestId, String adUnitId, ReadableMap adRequestOp @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { WritableMap error = Arguments.createMap(); - String[] codeAndMessage = getCodeAndMessageFromAdErrorCode(loadAdError); + String[] codeAndMessage = getCodeAndMessageFromAdError(loadAdError); error.putString("code", codeAndMessage[0]); error.putString("message", codeAndMessage[1]); sendRewardedEvent(GOOGLE_ADS_EVENT_ERROR, requestId, adUnitId, error, null); From b581d369f7539bbe66a4fd1d16b85a8545ec014c Mon Sep 17 00:00:00 2001 From: Dylan Companjen Date: Wed, 29 Dec 2021 15:46:48 +0100 Subject: [PATCH 8/8] fix(android): formatting --- .../java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java index 9a621833..b8465568 100644 --- a/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java +++ b/android/src/main/java/io/invertase/googleads/ReactNativeGoogleAdsCommon.java @@ -28,9 +28,9 @@ import com.facebook.react.bridge.WritableMap; import com.facebook.react.views.view.ReactViewGroup; import com.google.ads.mediation.admob.AdMobAdapter; +import com.google.android.gms.ads.AdError; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdSize; -import com.google.android.gms.ads.AdError; import io.invertase.googleads.common.ReactNativeEventEmitter; import java.util.ArrayList; import java.util.Map;