Skip to content

Commit 23e5998

Browse files
authored
fix(admob, android): unity ads require Activity Context (#4921)
* fix unity mediation Unity Ads requires an Activity context to load ads. * fix adContext for interstitial ad * fall back to applicationContext * reuse activity var, remove context fallback on null activity
1 parent e5ee66b commit 23e5998

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

packages/admob/android/src/main/java/io/invertase/firebase/admob/ReactNativeFirebaseAdMobInterstitialModule.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*
1818
*/
1919

20+
import android.app.Activity;
2021
import android.util.SparseArray;
2122

2223
import com.facebook.react.bridge.Arguments;
@@ -63,15 +64,16 @@ private void sendInterstitialEvent(String type, int requestId, String adUnitId,
6364

6465
@ReactMethod
6566
public void interstitialLoad(int requestId, String adUnitId, ReadableMap adRequestOptions) {
66-
if (getCurrentActivity() == null) {
67+
Activity currentActivity = getCurrentActivity();
68+
if (currentActivity == null) {
6769
WritableMap error = Arguments.createMap();
6870
error.putString("code", "null-activity");
6971
error.putString("message", "Interstitial ad attempted to load but the current Activity was null.");
7072
sendInterstitialEvent(AD_ERROR, requestId, adUnitId, error);
7173
return;
7274
}
73-
getCurrentActivity().runOnUiThread(() -> {
74-
InterstitialAd interstitialAd = new InterstitialAd(getApplicationContext());
75+
currentActivity.runOnUiThread(() -> {
76+
InterstitialAd interstitialAd = new InterstitialAd(currentActivity);
7577
interstitialAd.setAdUnitId(adUnitId);
7678

7779
// Apply AdRequest builder

packages/admob/android/src/main/java/io/invertase/firebase/admob/ReactNativeFirebaseAdMobRewardedModule.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.invertase.firebase.admob;
22

3+
import android.app.Activity;
34
import android.util.SparseArray;
45

56
import androidx.annotation.NonNull;
@@ -51,15 +52,16 @@ private void sendRewardedEvent(String type, int requestId, String adUnitId, @Nul
5152

5253
@ReactMethod
5354
public void rewardedLoad(int requestId, String adUnitId, ReadableMap adRequestOptions) {
54-
if (getCurrentActivity() == null) {
55+
Activity activity = getCurrentActivity();
56+
if (activity == null) {
5557
WritableMap error = Arguments.createMap();
5658
error.putString("code", "null-activity");
5759
error.putString("message", "Rewarded ad attempted to load but the current Activity was null.");
5860
sendRewardedEvent(AD_ERROR, requestId, adUnitId, error, null);
5961
return;
6062
}
61-
getCurrentActivity().runOnUiThread(() -> {
62-
RewardedAd rewardedAd = new RewardedAd(getApplicationContext(), adUnitId);
63+
activity.runOnUiThread(() -> {
64+
RewardedAd rewardedAd = new RewardedAd(activity, adUnitId);
6365

6466
RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
6567
@Override

0 commit comments

Comments
 (0)