Mauricio Castro
Mauricio Castro

Reputation: 154

Rewarded Ads NOT LOADING

Hello,

I'm trying to display a rewarded ad, but it fails to load. I followed AdMob's official tutorial(https://www.youtube.com/watch?v=CmrTF0hLsIk&t). This is the error:

E/Ads: Google Mobile Ads SDK initialization functionality unavailable for this session. Ad requests can be made at any time.

I/Ads: This request is sent from a test device.

This is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reward);

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });

    loadAd();
}

public void loadAd(){

    this.rewardedAd = new RewardedAd(this, "ca-app-pub-3940256099942544/5224354917");
    RewardedAdLoadCallback callback = new RewardedAdLoadCallback(){

        @Override
        public void onRewardedAdFailedToLoad(int i) {
            super.onRewardedAdFailedToLoad(i);

            Log.i(TAG, "OnRewardedAdFailedToLoad");
        }

        @Override
        public void onRewardedAdLoaded() {
            super.onRewardedAdLoaded();

            Log.i(TAG, "OnRewardedTagLoaded");
        }
    };
    this.rewardedAd.loadAd(new AdRequest.Builder().build(), callback);
}



public void showAd(){

    if (this.rewardedAd.isLoaded()){

        RewardedAdCallback callback = new RewardedAdCallback() {
            @Override
            public void onUserEarnedReward(@NonNull RewardItem rewardItem) {

                Log.i(TAG, "OnUserEarnedReward");
            }

            @Override
            public void onRewardedAdOpened() {
                super.onRewardedAdOpened();

                Log.i(TAG, "OnRewardAdOpened");
            }

            @Override
            public void onRewardedAdClosed() {
                super.onRewardedAdClosed();

                Log.i(TAG, "OnRewardAdClosed");
            }

            @Override
            public void onRewardedAdFailedToShow(int i) {
                super.onRewardedAdFailedToShow(i);

                Log.i(TAG, "OnRewardedAdFailedToShow");
            }
        };
        this.rewardedAd.show(this, callback);

    } else {

        Log.i(TAG, "Ad not loaded.");
    }
}

All AdMob IDs used are just for a test, I got them in AdMob docs

In manifest, I've added:

<meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-3940256099942544~3347511713" />

build.gradle:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.0.0'

implementation 'com.google.android.gms:play-services-ads:18.2.0'
implementation 'com.google.firebase:firebase-database:17.0.0'

implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
apply plugin: 'com.google.gms.google-services'

Please help me, I only need to fix this to finish my app. I've been stuck in this error for two weeks, I'm very tired and I'm thinking about giving up.

Upvotes: 1

Views: 1768

Answers (2)

Mauricio Castro
Mauricio Castro

Reputation: 154

SOLUTION:

The error was in the emulator! To load ads, we need wi-fi connection. AVDs with API =< 25 cannot connect to wi-fi. So I just installed Android Oreo and it worked.

Upvotes: 1

wang willway
wang willway

Reputation: 505

Have you ever added test device id? It is easy to find test device id by filtering AdRequest.Builder.addTestDevice in Logcat.

Upvotes: 0

Related Questions