react-native-google-mobile-ads 6.2.4 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/RNGoogleMobileAds.podspec +1 -1
  2. package/android/build.gradle +23 -3
  3. package/android/src/main/AndroidManifest.xml +6 -0
  4. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsAdHelper.kt +106 -0
  5. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsAppOpenModule.kt +74 -0
  6. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsCommon.java +5 -0
  7. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsFullScreenAdModule.kt +245 -0
  8. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsInterstitialModule.kt +74 -0
  9. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsModule.java +2 -1
  10. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsRewardedInterstitialModule.kt +74 -0
  11. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsRewardedModule.kt +74 -0
  12. package/docs/displaying-ads.mdx +2 -2
  13. package/docs/european-user-consent.mdx +3 -2
  14. package/docs/index.mdx +5 -1
  15. package/docs/migrating-to-v6.mdx +2 -2
  16. package/ios/RNGoogleMobileAds/RNGoogleMobileAds-Bridging-Header.h +5 -0
  17. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsAppOpenModule.m +13 -68
  18. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsAppOpenModule.swift +82 -0
  19. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsCommon.m +4 -0
  20. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsFullScreenAd.swift +208 -0
  21. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsFullScreenContentDelegate.swift +72 -0
  22. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsInterstitialModule.m +13 -107
  23. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsInterstitialModule.swift +119 -0
  24. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsModule.m +2 -1
  25. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedInterstitialModule.m +13 -144
  26. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedInterstitialModule.swift +81 -0
  27. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedModule.m +13 -143
  28. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedModule.swift +81 -0
  29. package/ios_config.sh +10 -0
  30. package/lib/commonjs/ads/MobileAd.js +1 -1
  31. package/lib/commonjs/ads/MobileAd.js.map +1 -1
  32. package/lib/commonjs/validateAdRequestOptions.js +8 -0
  33. package/lib/commonjs/validateAdRequestOptions.js.map +1 -1
  34. package/lib/commonjs/version.js +1 -1
  35. package/lib/commonjs/version.js.map +1 -1
  36. package/lib/module/ads/MobileAd.js +1 -1
  37. package/lib/module/ads/MobileAd.js.map +1 -1
  38. package/lib/module/validateAdRequestOptions.js +8 -0
  39. package/lib/module/validateAdRequestOptions.js.map +1 -1
  40. package/lib/module/version.js +1 -1
  41. package/lib/module/version.js.map +1 -1
  42. package/lib/typescript/index.d.ts +1 -1
  43. package/lib/typescript/types/RequestOptions.d.ts +6 -0
  44. package/lib/typescript/version.d.ts +1 -1
  45. package/package.json +4 -4
  46. package/src/ads/MobileAd.ts +2 -1
  47. package/src/types/RequestOptions.ts +7 -0
  48. package/src/validateAdRequestOptions.ts +7 -0
  49. package/src/version.ts +1 -1
  50. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsAppOpenModule.java +0 -159
  51. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsInterstitialModule.java +0 -178
  52. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsRewardedInterstitialModule.java +0 -199
  53. package/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsRewardedModule.java +0 -176
  54. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsAppOpenModule.h +0 -29
  55. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsFullScreenContentDelegate.h +0 -33
  56. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsFullScreenContentDelegate.m +0 -72
  57. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsInterstitialModule.h +0 -25
  58. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedInterstitialModule.h +0 -25
  59. package/ios/RNGoogleMobileAds/RNGoogleMobileAdsRewardedModule.h +0 -25
@@ -93,4 +93,11 @@ export interface RequestOptions {
93
93
  * See [Google Mobile SDK Docs](https://developers.google.com/admob/android/ssv) for more information.
94
94
  */
95
95
  serverSideVerificationOptions?: ServerSideVerificationOptions;
96
+
97
+ /**
98
+ * Publisher provided identifier (PPID) for use in frequency capping, audience segmentation and targeting,
99
+ * sequential ad rotation, and other audience-based ad delivery controls across devices.
100
+ * See [this article](https://support.google.com/admanager/answer/2880055) for more information.
101
+ */
102
+ publisherProvidedId?: string;
96
103
  }
@@ -126,5 +126,12 @@ export function validateAdRequestOptions(options?: RequestOptions) {
126
126
  out.customTargeting = options.customTargeting;
127
127
  }
128
128
 
129
+ if (options.publisherProvidedId) {
130
+ if (!isString(options.publisherProvidedId)) {
131
+ throw new Error("'options.publisherProvidedId' expected a string value");
132
+ }
133
+ out.publisherProvidedId = options.publisherProvidedId;
134
+ }
135
+
129
136
  return out;
130
137
  }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '6.2.4';
2
+ export const version = '7.0.0';
@@ -1,159 +0,0 @@
1
- package io.invertase.googlemobileads;
2
-
3
- /*
4
- * Copyright (c) 2016-present Invertase Limited & Contributors
5
- *
6
- * Licensed under the Apache License, Version 2.0 (the "License");
7
- * you may not use this library except in compliance with the License.
8
- * You may obtain a copy of the License at
9
- *
10
- * http://www.apache.org/licenses/LICENSE-2.0
11
- *
12
- * Unless required by applicable law or agreed to in writing, software
13
- * distributed under the License is distributed on an "AS IS" BASIS,
14
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- * See the License for the specific language governing permissions and
16
- * limitations under the License.
17
- *
18
- */
19
-
20
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsCommon.buildAdRequest;
21
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsCommon.getCodeAndMessageFromAdError;
22
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsCommon.sendAdEvent;
23
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_APP_OPEN;
24
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_CLICKED;
25
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_CLOSED;
26
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_ERROR;
27
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_LOADED;
28
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_OPENED;
29
-
30
- import android.app.Activity;
31
- import androidx.annotation.NonNull;
32
- import com.facebook.react.bridge.Arguments;
33
- import com.facebook.react.bridge.Promise;
34
- import com.facebook.react.bridge.ReactApplicationContext;
35
- import com.facebook.react.bridge.ReactMethod;
36
- import com.facebook.react.bridge.ReadableMap;
37
- import com.facebook.react.bridge.WritableMap;
38
- import com.google.android.gms.ads.FullScreenContentCallback;
39
- import com.google.android.gms.ads.LoadAdError;
40
- import com.google.android.gms.ads.appopen.AppOpenAd;
41
- import io.invertase.googlemobileads.common.ReactNativeModule;
42
- import javax.annotation.Nullable;
43
-
44
- public class ReactNativeGoogleMobileAdsAppOpenModule extends ReactNativeModule {
45
- private static final String SERVICE = "RNGoogleMobileAdsAppOpenModule";
46
- private AppOpenAd appOpenAd = null;
47
-
48
- public ReactNativeGoogleMobileAdsAppOpenModule(ReactApplicationContext reactContext) {
49
- super(reactContext, SERVICE);
50
- }
51
-
52
- private void sendAppOpenEvent(
53
- String type, int requestId, String adUnitId, @Nullable WritableMap error) {
54
- sendAdEvent(GOOGLE_MOBILE_ADS_EVENT_APP_OPEN, requestId, type, adUnitId, error);
55
- }
56
-
57
- @ReactMethod
58
- public void appOpenLoad(int requestId, String adUnitId, ReadableMap adRequestOptions) {
59
- Activity currentActivity = getCurrentActivity();
60
- if (currentActivity == null) {
61
- WritableMap error = Arguments.createMap();
62
- error.putString("code", "null-activity");
63
- error.putString(
64
- "message", "App Open ad attempted to load but the current Activity was null.");
65
- sendAppOpenEvent(GOOGLE_MOBILE_ADS_EVENT_ERROR, requestId, adUnitId, error);
66
- return;
67
- }
68
- currentActivity.runOnUiThread(
69
- () -> {
70
- AppOpenAd.AppOpenAdLoadCallback appOpenAdLoadCallback =
71
- new AppOpenAd.AppOpenAdLoadCallback() {
72
-
73
- @Override
74
- public void onAdLoaded(@NonNull AppOpenAd appOpenAd) {
75
-
76
- appOpenAd.setFullScreenContentCallback(
77
- new FullScreenContentCallback() {
78
- @Override
79
- public void onAdDismissedFullScreenContent() {
80
- sendAppOpenEvent(
81
- GOOGLE_MOBILE_ADS_EVENT_CLOSED, requestId, adUnitId, null);
82
- ReactNativeGoogleMobileAdsAppOpenModule.this.appOpenAd = null;
83
- }
84
-
85
- @Override
86
- public void onAdClicked() {
87
- sendAppOpenEvent(
88
- GOOGLE_MOBILE_ADS_EVENT_CLICKED, requestId, adUnitId, null);
89
- }
90
-
91
- @Override
92
- public void onAdShowedFullScreenContent() {
93
- sendAppOpenEvent(
94
- GOOGLE_MOBILE_ADS_EVENT_OPENED, requestId, adUnitId, null);
95
- }
96
- });
97
-
98
- ReactNativeGoogleMobileAdsAppOpenModule.this.appOpenAd = appOpenAd;
99
- sendAppOpenEvent(GOOGLE_MOBILE_ADS_EVENT_LOADED, requestId, adUnitId, null);
100
- }
101
-
102
- @Override
103
- public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
104
- WritableMap error = Arguments.createMap();
105
- String[] codeAndMessage = getCodeAndMessageFromAdError(loadAdError);
106
- error.putString("code", codeAndMessage[0]);
107
- error.putString("message", codeAndMessage[1]);
108
- sendAppOpenEvent(GOOGLE_MOBILE_ADS_EVENT_ERROR, requestId, adUnitId, error);
109
- }
110
- };
111
-
112
- AppOpenAd.load(
113
- currentActivity,
114
- adUnitId,
115
- buildAdRequest(adRequestOptions),
116
- AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT,
117
- appOpenAdLoadCallback);
118
- });
119
- }
120
-
121
- @ReactMethod
122
- public void appOpenShow(
123
- int requestId, String adUnitId, ReadableMap showOptions, Promise promise) {
124
- if (getCurrentActivity() == null) {
125
- rejectPromiseWithCodeAndMessage(
126
- promise,
127
- "null-activity",
128
- "App Open ad attempted to show but the current Activity was null.");
129
- return;
130
- }
131
- getCurrentActivity()
132
- .runOnUiThread(
133
- () -> {
134
- if (appOpenAd == null) {
135
- rejectPromiseWithCodeAndMessage(
136
- promise,
137
- "null-appOpenAd",
138
- "App Open ad attempted to show but its object was null.");
139
- return;
140
- }
141
-
142
- if (showOptions.hasKey("immersiveModeEnabled")) {
143
- appOpenAd.setImmersiveMode(showOptions.getBoolean("immersiveModeEnabled"));
144
- } else {
145
- appOpenAd.setImmersiveMode(false);
146
- }
147
-
148
- String a = String.valueOf(requestId);
149
-
150
- if (appOpenAd != null) {
151
- appOpenAd.show(getCurrentActivity());
152
- promise.resolve(null);
153
- } else {
154
- rejectPromiseWithCodeAndMessage(
155
- promise, "not-ready", "App Open ad attempted to show but was not ready.");
156
- }
157
- });
158
- }
159
- }
@@ -1,178 +0,0 @@
1
- package io.invertase.googlemobileads;
2
-
3
- /*
4
- * Copyright (c) 2016-present Invertase Limited & Contributors
5
- *
6
- * Licensed under the Apache License, Version 2.0 (the "License");
7
- * you may not use this library except in compliance with the License.
8
- * You may obtain a copy of the License at
9
- *
10
- * http://www.apache.org/licenses/LICENSE-2.0
11
- *
12
- * Unless required by applicable law or agreed to in writing, software
13
- * distributed under the License is distributed on an "AS IS" BASIS,
14
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- * See the License for the specific language governing permissions and
16
- * limitations under the License.
17
- *
18
- */
19
-
20
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsCommon.buildAdRequest;
21
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsCommon.getCodeAndMessageFromAdError;
22
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsCommon.sendAdEvent;
23
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_APP_EVENT;
24
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_CLICKED;
25
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_CLOSED;
26
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_ERROR;
27
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_INTERSTITIAL;
28
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_LOADED;
29
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_OPENED;
30
-
31
- import android.app.Activity;
32
- import android.util.SparseArray;
33
- import androidx.annotation.NonNull;
34
- import com.facebook.react.bridge.Arguments;
35
- import com.facebook.react.bridge.Promise;
36
- import com.facebook.react.bridge.ReactApplicationContext;
37
- import com.facebook.react.bridge.ReactMethod;
38
- import com.facebook.react.bridge.ReadableMap;
39
- import com.facebook.react.bridge.WritableMap;
40
- import com.google.android.gms.ads.FullScreenContentCallback;
41
- import com.google.android.gms.ads.LoadAdError;
42
- import com.google.android.gms.ads.admanager.AdManagerInterstitialAd;
43
- import com.google.android.gms.ads.admanager.AppEventListener;
44
- import com.google.android.gms.ads.interstitial.InterstitialAd;
45
- import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
46
- import io.invertase.googlemobileads.common.ReactNativeModule;
47
- import javax.annotation.Nullable;
48
-
49
- public class ReactNativeGoogleMobileAdsInterstitialModule extends ReactNativeModule {
50
- private static final String SERVICE = "RNGoogleMobileAdsInterstitialModule";
51
- private static SparseArray<InterstitialAd> interstitialAdArray = new SparseArray<>();
52
-
53
- public ReactNativeGoogleMobileAdsInterstitialModule(ReactApplicationContext reactContext) {
54
- super(reactContext, SERVICE);
55
- }
56
-
57
- private void sendInterstitialEvent(
58
- String type, int requestId, String adUnitId, @Nullable WritableMap error) {
59
- sendAdEvent(GOOGLE_MOBILE_ADS_EVENT_INTERSTITIAL, requestId, type, adUnitId, error);
60
- }
61
-
62
- @ReactMethod
63
- public void interstitialLoad(int requestId, String adUnitId, ReadableMap adRequestOptions) {
64
- Activity currentActivity = getCurrentActivity();
65
- if (currentActivity == null) {
66
- WritableMap error = Arguments.createMap();
67
- error.putString("code", "null-activity");
68
- error.putString(
69
- "message", "Interstitial ad attempted to load but the current Activity was null.");
70
- sendInterstitialEvent(GOOGLE_MOBILE_ADS_EVENT_ERROR, requestId, adUnitId, error);
71
- return;
72
- }
73
- currentActivity.runOnUiThread(
74
- () -> {
75
- InterstitialAdLoadCallback interstitialAdLoadCallback =
76
- new InterstitialAdLoadCallback() {
77
-
78
- @Override
79
- public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
80
- interstitialAd.setFullScreenContentCallback(
81
- new FullScreenContentCallback() {
82
- @Override
83
- public void onAdDismissedFullScreenContent() {
84
- sendInterstitialEvent(
85
- GOOGLE_MOBILE_ADS_EVENT_CLOSED, requestId, adUnitId, null);
86
- interstitialAdArray.put(requestId, null);
87
- }
88
-
89
- @Override
90
- public void onAdClicked() {
91
- sendInterstitialEvent(
92
- GOOGLE_MOBILE_ADS_EVENT_CLICKED, requestId, adUnitId, null);
93
- }
94
-
95
- @Override
96
- public void onAdShowedFullScreenContent() {
97
- sendInterstitialEvent(
98
- GOOGLE_MOBILE_ADS_EVENT_OPENED, requestId, adUnitId, null);
99
- }
100
- });
101
- if (interstitialAd instanceof AdManagerInterstitialAd) {
102
- ((AdManagerInterstitialAd) interstitialAd)
103
- .setAppEventListener(
104
- new AppEventListener() {
105
- @Override
106
- public void onAppEvent(@NonNull String name, @Nullable String data) {
107
- WritableMap payload = Arguments.createMap();
108
- payload.putString("name", name);
109
- payload.putString("data", data);
110
- sendInterstitialEvent(
111
- GOOGLE_MOBILE_ADS_EVENT_APP_EVENT,
112
- requestId,
113
- adUnitId,
114
- payload);
115
- }
116
- });
117
- }
118
- interstitialAdArray.put(requestId, interstitialAd);
119
- sendInterstitialEvent(GOOGLE_MOBILE_ADS_EVENT_LOADED, requestId, adUnitId, null);
120
- }
121
-
122
- @Override
123
- public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
124
- WritableMap error = Arguments.createMap();
125
- String[] codeAndMessage = getCodeAndMessageFromAdError(loadAdError);
126
- error.putString("code", codeAndMessage[0]);
127
- error.putString("message", codeAndMessage[1]);
128
- sendInterstitialEvent(GOOGLE_MOBILE_ADS_EVENT_ERROR, requestId, adUnitId, error);
129
- }
130
- };
131
-
132
- if (ReactNativeGoogleMobileAdsCommon.isAdManagerUnit(adUnitId)) {
133
- AdManagerInterstitialAd.load(
134
- currentActivity,
135
- adUnitId,
136
- buildAdRequest(adRequestOptions),
137
- interstitialAdLoadCallback);
138
- } else {
139
- InterstitialAd.load(
140
- currentActivity,
141
- adUnitId,
142
- buildAdRequest(adRequestOptions),
143
- interstitialAdLoadCallback);
144
- }
145
- });
146
- }
147
-
148
- @ReactMethod
149
- public void interstitialShow(
150
- int requestId, String adUnitId, ReadableMap showOptions, Promise promise) {
151
- if (getCurrentActivity() == null) {
152
- rejectPromiseWithCodeAndMessage(
153
- promise,
154
- "null-activity",
155
- "Interstitial ad attempted to show but the current Activity was null.");
156
- return;
157
- }
158
- getCurrentActivity()
159
- .runOnUiThread(
160
- () -> {
161
- InterstitialAd interstitialAd = interstitialAdArray.get(requestId);
162
- if (interstitialAd == null) {
163
- rejectPromiseWithCodeAndMessage(
164
- promise, "not-ready", "Interstitial ad attempted to show but was not ready.");
165
- return;
166
- }
167
-
168
- if (showOptions.hasKey("immersiveModeEnabled")) {
169
- interstitialAd.setImmersiveMode(showOptions.getBoolean("immersiveModeEnabled"));
170
- } else {
171
- interstitialAd.setImmersiveMode(false);
172
- }
173
-
174
- interstitialAd.show(getCurrentActivity());
175
- promise.resolve(null);
176
- });
177
- }
178
- }
@@ -1,199 +0,0 @@
1
- package io.invertase.googlemobileads;
2
-
3
- /*
4
- * Copyright (c) 2016-present Invertase Limited & Contributors
5
- *
6
- * Licensed under the Apache License, Version 2.0 (the "License");
7
- * you may not use this library except in compliance with the License.
8
- * You may obtain a copy of the License at
9
- *
10
- * http://www.apache.org/licenses/LICENSE-2.0
11
- *
12
- * Unless required by applicable law or agreed to in writing, software
13
- * distributed under the License is distributed on an "AS IS" BASIS,
14
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- * See the License for the specific language governing permissions and
16
- * limitations under the License.
17
- *
18
- */
19
-
20
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsCommon.buildAdRequest;
21
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsCommon.getCodeAndMessageFromAdError;
22
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsCommon.sendAdEvent;
23
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_CLOSED;
24
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_ERROR;
25
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_OPENED;
26
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_REWARDED_EARNED_REWARD;
27
- import static io.invertase.googlemobileads.ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_REWARDED_LOADED;
28
-
29
- import android.app.Activity;
30
- import android.util.SparseArray;
31
- import androidx.annotation.NonNull;
32
- import androidx.annotation.Nullable;
33
- import com.facebook.react.bridge.Arguments;
34
- import com.facebook.react.bridge.Promise;
35
- import com.facebook.react.bridge.ReactApplicationContext;
36
- import com.facebook.react.bridge.ReactMethod;
37
- import com.facebook.react.bridge.ReadableMap;
38
- import com.facebook.react.bridge.WritableMap;
39
- import com.google.android.gms.ads.FullScreenContentCallback;
40
- import com.google.android.gms.ads.LoadAdError;
41
- import com.google.android.gms.ads.OnUserEarnedRewardListener;
42
- import com.google.android.gms.ads.rewarded.RewardItem;
43
- import com.google.android.gms.ads.rewarded.ServerSideVerificationOptions;
44
- import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;
45
- import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback;
46
- import io.invertase.googlemobileads.common.ReactNativeModule;
47
-
48
- public class ReactNativeGoogleMobileAdsRewardedInterstitialModule extends ReactNativeModule {
49
- private static final String SERVICE = "RNGoogleMobileAdsRewardedInterstitialModule";
50
- private static SparseArray<RewardedInterstitialAd> rewardedInterstitialAdArray =
51
- new SparseArray<>();
52
-
53
- public ReactNativeGoogleMobileAdsRewardedInterstitialModule(
54
- ReactApplicationContext reactContext) {
55
- super(reactContext, SERVICE);
56
- }
57
-
58
- private void sendRewardedInterstitialEvent(
59
- String type,
60
- int requestId,
61
- String adUnitId,
62
- @Nullable WritableMap error,
63
- @Nullable WritableMap data) {
64
- sendAdEvent(
65
- ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_REWARDED_INTERSTITIAL,
66
- requestId,
67
- type,
68
- adUnitId,
69
- error,
70
- data);
71
- }
72
-
73
- @ReactMethod
74
- public void rewardedInterstitialLoad(
75
- int requestId, String adUnitId, ReadableMap adRequestOptions) {
76
- Activity activity = getCurrentActivity();
77
- if (activity == null) {
78
- WritableMap error = Arguments.createMap();
79
- error.putString("code", "null-activity");
80
- error.putString(
81
- "message",
82
- "Rewarded Interstitial ad attempted to load but the current Activity was null.");
83
- sendRewardedInterstitialEvent(
84
- GOOGLE_MOBILE_ADS_EVENT_ERROR, requestId, adUnitId, error, null);
85
- return;
86
- }
87
- activity.runOnUiThread(
88
- () -> {
89
- RewardedInterstitialAdLoadCallback rewardedAdLoadCallback =
90
- new RewardedInterstitialAdLoadCallback() {
91
- @Override
92
- public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
93
- WritableMap error = Arguments.createMap();
94
- String[] codeAndMessage = getCodeAndMessageFromAdError(loadAdError);
95
- error.putString("code", codeAndMessage[0]);
96
- error.putString("message", codeAndMessage[1]);
97
- sendRewardedInterstitialEvent(
98
- GOOGLE_MOBILE_ADS_EVENT_ERROR, requestId, adUnitId, error, null);
99
- }
100
-
101
- @Override
102
- public void onAdLoaded(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
103
- RewardItem rewardItem = rewardedInterstitialAd.getRewardItem();
104
- WritableMap data = Arguments.createMap();
105
- data.putString("type", rewardItem.getType());
106
- data.putInt("amount", rewardItem.getAmount());
107
-
108
- if (adRequestOptions.hasKey("serverSideVerificationOptions")) {
109
- ReadableMap serverSideVerificationOptions =
110
- adRequestOptions.getMap("serverSideVerificationOptions");
111
-
112
- if (serverSideVerificationOptions != null) {
113
- ServerSideVerificationOptions.Builder options =
114
- new ServerSideVerificationOptions.Builder();
115
-
116
- if (serverSideVerificationOptions.hasKey("userId")) {
117
- options.setUserId(serverSideVerificationOptions.getString("userId"));
118
- }
119
-
120
- if (serverSideVerificationOptions.hasKey("customData")) {
121
- options.setCustomData(
122
- serverSideVerificationOptions.getString("customData"));
123
- }
124
-
125
- rewardedInterstitialAd.setServerSideVerificationOptions(options.build());
126
- }
127
- }
128
-
129
- FullScreenContentCallback fullScreenContentCallback =
130
- new FullScreenContentCallback() {
131
- @Override
132
- public void onAdShowedFullScreenContent() {
133
- sendRewardedInterstitialEvent(
134
- GOOGLE_MOBILE_ADS_EVENT_OPENED, requestId, adUnitId, null, null);
135
- }
136
-
137
- @Override
138
- public void onAdDismissedFullScreenContent() {
139
- sendRewardedInterstitialEvent(
140
- GOOGLE_MOBILE_ADS_EVENT_CLOSED, requestId, adUnitId, null, null);
141
- }
142
- };
143
-
144
- rewardedInterstitialAd.setFullScreenContentCallback(fullScreenContentCallback);
145
-
146
- rewardedInterstitialAdArray.put(requestId, rewardedInterstitialAd);
147
- sendRewardedInterstitialEvent(
148
- GOOGLE_MOBILE_ADS_EVENT_REWARDED_LOADED, requestId, adUnitId, null, data);
149
- }
150
- };
151
-
152
- RewardedInterstitialAd.load(
153
- activity, adUnitId, buildAdRequest(adRequestOptions), rewardedAdLoadCallback);
154
- });
155
- }
156
-
157
- @ReactMethod
158
- public void rewardedInterstitialShow(
159
- int requestId, String adUnitId, ReadableMap showOptions, Promise promise) {
160
- if (getCurrentActivity() == null) {
161
- rejectPromiseWithCodeAndMessage(
162
- promise,
163
- "null-activity",
164
- "Rewarded Interstitial ad attempted to show but the current Activity was null.");
165
- return;
166
- }
167
- getCurrentActivity()
168
- .runOnUiThread(
169
- () -> {
170
- RewardedInterstitialAd rewardedInterstitialAd =
171
- rewardedInterstitialAdArray.get(requestId);
172
-
173
- boolean immersiveModeEnabled = false;
174
- if (showOptions.hasKey("immersiveModeEnabled")) {
175
- immersiveModeEnabled = showOptions.getBoolean("immersiveModeEnabled");
176
- }
177
- rewardedInterstitialAd.setImmersiveMode(immersiveModeEnabled);
178
-
179
- OnUserEarnedRewardListener onUserEarnedRewardListener =
180
- new OnUserEarnedRewardListener() {
181
- @Override
182
- public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
183
- WritableMap data = Arguments.createMap();
184
- data.putString("type", rewardItem.getType());
185
- data.putInt("amount", rewardItem.getAmount());
186
- sendRewardedInterstitialEvent(
187
- GOOGLE_MOBILE_ADS_EVENT_REWARDED_EARNED_REWARD,
188
- requestId,
189
- adUnitId,
190
- null,
191
- data);
192
- }
193
- };
194
-
195
- rewardedInterstitialAd.show(getCurrentActivity(), onUserEarnedRewardListener);
196
- promise.resolve(null);
197
- });
198
- }
199
- }