5

I'm trying to load admob native ads but I just can't. I can't see what is the problem. Spent 3 days on search and dont know where the problem is... Here is code for loading an ads:

private void getNativeAds() {
    _nativeAdmobMutable = ViewModelProviders.of(this).get(MutableNativeADModel.class);

    AdLoader.Builder builder = new AdLoader.Builder(this, GlobalConstants.numberForNativeAdmob);
    adLoader = builder.forUnifiedNativeAd(
            new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                @Override
                public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                    _nativeAds.add(unifiedNativeAd);
                    if (!adLoader.isLoading()) {
                        _nativeAdmobMutable.setAdmobNativeAd(_nativeAds);
                    }
                    if (isDestroyed()) {
                        unifiedNativeAd.destroy();
                        return;
                    }
                }
            }).withAdListener(
            new AdListener() {

                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                }
                @Override
                public void onAdFailedToLoad(LoadAdError errorCode) {
                    Log.d("ERROR ", errorCode.getMessage());
                    if (!adLoader.isLoading()) {
                        _nativeAdmobMutable.setAdmobNativeAd(_nativeAds);
                    }
                }
            }).withNativeAdOptions(new NativeAdOptions.Builder()
                    .build())
            .build();

    adLoader.loadAds(new AdRequest.Builder().build(), 5);

}

this method is called after

MobileAds.initialize(this, new OnInitializationCompleteListener() ... etc

this are my dependencies

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.exoplayer:exoplayer:2.10.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha05'
implementation 'com.intuit.sdp:sdp-android:1.0.6'
implementation 'com.google.android.gms:play-services-ads:19.4.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.picasso:picasso:2.71828'

Everytime onAdFailedToLoad callback is called with log:

I/Ads: Ad failed to load : 0
D/ERROR: Invalid template ID: -1

I also tried to load single ad like:

adLoader.loadAd(new AdRequest.Builder().build());

and then I get error No ads config. and I/Ads: Ad failed to load : 3

Does anyone have some idea what could be the problem here ? I also have implemented banner, interstitial and video reward, they work just fine (app is live for almost a year on GP). Ofcourse i'm using TEST ID for ads, not real one. Thanks in advance !

EDIT 1: Here is layout

<com.google.android.gms.ads.formats.UnifiedNativeAdView
android:id="@+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FFFFFF"
        android:minHeight="50dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/ad_attribution"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:textColor="#FFFFFF"
            android:textSize="12sp"
            android:text="Ad"
            android:background="#FFCC66"
            android:width="15dp"
            android:height="15dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="3dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/ad_icon"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:adjustViewBounds="true"
                    android:paddingBottom="5dp"
                    android:paddingRight="5dp"
                    android:paddingEnd="5dp"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/ad_headline"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColor="#0000FF"
                        android:textSize="16sp"
                        android:textStyle="bold" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/ad_advertiser"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:gravity="bottom"
                            android:textSize="14sp"
                            android:textStyle="bold"/>

                        <RatingBar
                            android:id="@+id/ad_stars"
                            style="?android:attr/ratingBarStyleSmall"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:isIndicator="true"
                            android:numStars="5"
                            android:stepSize="0.5" />
                    </LinearLayout>

                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/ad_body"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="20dp"
                    android:layout_marginEnd="20dp"
                    android:textSize="12sp" />

                <com.google.android.gms.ads.formats.MediaView
                    android:id="@+id/ad_media"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="250dp"
                    android:layout_height="175dp"
                    android:layout_marginTop="5dp" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end"
                    android:orientation="horizontal"
                    android:paddingBottom="10dp"
                    android:paddingTop="10dp">

                    <TextView
                        android:id="@+id/ad_price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="5dp"
                        android:paddingStart="5dp"
                        android:paddingRight="5dp"
                        android:paddingEnd="5dp"
                        android:textSize="12sp" />

                    <TextView
                        android:id="@+id/ad_store"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="5dp"
                        android:paddingStart="5dp"
                        android:paddingRight="5dp"
                        android:paddingEnd="5dp"
                        android:textSize="12sp" />

                    <Button
                        android:id="@+id/ad_call_to_action"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="12sp" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</com.google.android.gms.ads.formats.UnifiedNativeAdView>

Loading ad i allready wrote above. This is how to connect layout

 public UnifiedNativeAdViewHolder(@NonNull View itemView) {
    super(itemView);
    adView = (UnifiedNativeAdView) itemView.findViewById(R.id.ad_view);

    // The MediaView will display a video asset if one is present in the ad, and the
    // first image asset otherwise.
    adView.setMediaView((MediaView) adView.findViewById(R.id.ad_media));

    // Register the view used for each individual asset.
    adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
    adView.setBodyView(adView.findViewById(R.id.ad_body));
    adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
    adView.setIconView(adView.findViewById(R.id.ad_icon));
    adView.setPriceView(adView.findViewById(R.id.ad_price));
    adView.setStarRatingView(adView.findViewById(R.id.ad_stars));
    adView.setStoreView(adView.findViewById(R.id.ad_store));
    adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
}

and populate like this

        // Some assets are guaranteed to be in every UnifiedNativeAd.
    ((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
    ((TextView) adView.getBodyView()).setText(nativeAd.getBody());
    ((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());

    // These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
    // check before trying to display them.
    NativeAd.Image icon = nativeAd.getIcon();

    if (icon == null) {
        adView.getIconView().setVisibility(View.INVISIBLE);
    } else {
        ((ImageView) adView.getIconView()).setImageDrawable(icon.getDrawable());
        adView.getIconView().setVisibility(View.VISIBLE);
    }

    if (nativeAd.getPrice() == null) {
        adView.getPriceView().setVisibility(View.INVISIBLE);
    } else {
        adView.getPriceView().setVisibility(View.VISIBLE);
        ((TextView) adView.getPriceView()).setText(nativeAd.getPrice());
    }

    if (nativeAd.getStore() == null) {
        adView.getStoreView().setVisibility(View.INVISIBLE);
    } else {
        adView.getStoreView().setVisibility(View.VISIBLE);
        ((TextView) adView.getStoreView()).setText(nativeAd.getStore());
    }

    if (nativeAd.getStarRating() == null) {
        adView.getStarRatingView().setVisibility(View.INVISIBLE);
    } else {
        ((RatingBar) adView.getStarRatingView())
                .setRating(nativeAd.getStarRating().floatValue());
        adView.getStarRatingView().setVisibility(View.VISIBLE);
    }

    if (nativeAd.getAdvertiser() == null) {
        adView.getAdvertiserView().setVisibility(View.INVISIBLE);
    } else {
        ((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
        adView.getAdvertiserView().setVisibility(View.VISIBLE);
    }

    // Assign native ad object to the native view.
    adView.setNativeAd(nativeAd);
Dusan Lilic
  • 102
  • 1
  • 8

2 Answers2

5

Could you also post your native ad layout, and how you inflate and populate the layout?

I'm facing the same problem, and the only way I've been able to get native ads is by NOT using the test ad ID's. With test IDs I get No ads config and I/Ads: Ad failed to load : 3. This is regardless of which of the two native test ad IDs I use. With the real ad ID and adding test unit IDs I get test ad but no media. Without test unit IDs I sometimes get a test ad from Facebook mediation, and other times the Invalid template ID: -1.

Edit Sept-2021 In the documentation for the test ad Ids a warning has been added about test ad ids and the use of an app-ads.txt file. Apparently, if you have app-ads.txt you need to add this line to it in order to use the demo ad ids:

google.com, pub-3940256099942544, DIRECT, f08c47fec0942fa0

I assume this was the problem for me, I never got the test ad ids to work, and had to add my phone as test device instead.

Leknesh
  • 307
  • 2
  • 10
  • yea sure, but tomorrow i'm not at house atm. I will post it in next 12 hours – Dusan Lilic Oct 05 '20 at 19:42
  • Also check their sample code here https://github.com/googleads/googleads-mobile-android-native-templates and take a look at documentation https://developers.google.com/admob/android/native/templates – Dusan Lilic Oct 06 '20 at 07:57
  • Also check again ur ID's, try both of numbers they give as test ads, Invalidate cache and restart project could also help sometimes. Also try to show them on different ways. For example import their template as view and try to show it like that. You have complete guide here https://codelabs.developers.google.com/codelabs/admob-native-templates-android/#4 and here https://codelabs.developers.google.com/codelabs/admob-native-advanced-feed-android/#1 – Dusan Lilic Oct 06 '20 at 08:01
  • ALSO, VERY IMPORTANT, kill app, delete cache, enter in app again, if u see native ads then ur implementation is potentially ok. Then kill app again and enter. If you don't see app on ur second and every another enter then you are not destroying ads properly and you should check that – Dusan Lilic Oct 06 '20 at 09:16
  • @DusanLilic your setup is pretty much exactly the same as mine. I realized the problem with missing media was a line from copied code where adOptions included `.setReturnUrlsForImageAssets(true)`. Without this, I get test ads with media if I add testdevices in requestconfiguration. I've tried using test ad IDs instead, tried both of the IDs from documentation (for image and video), but they don't work. Do I understand correctly that the test ad IDs work for you now? – Leknesh Oct 06 '20 at 14:29
  • Yup you understood me corectly. Test ad IDs work for me. Sorry for late resposne – Dusan Lilic Oct 09 '20 at 07:26
0

I was also facing this issue after testing on real device instead of emulator the error is gone. Please try that might help.

Ramesh Sanghar
  • 174
  • 1
  • 1
  • 19