I'm trying to add some Admob ads into my application. So far I've made the following code into the onCreate method.
long previousAdShown = this.prefs.getLong("last_ad_shown", 0);
long now = new Date().getTime();
if (Constants.ADMOB_ENABLE && (previousAdShown < now - Constants.ADMOB_INTERVAL))
{
Log.e("ADMOB", "Load ADMOB");
// Load ad view
this.adView = new InterstitialAd(this);
this.adView.setAdUnitId(Constants.ADMOB_ID);
AdRequest.Builder adRequest = new AdRequest.Builder();
adRequest.addTestDevice("31090DB31C8059FC6EF0FDA2XXXXXXXX");
this.adView.loadAd(adRequest.build());
// Update timer
this.prefs.edit().putLong("last_ad_shown", now).commit();
}
Which is supposed to pre-load the ads.
Then, in my Menu I got something like that:
this.menu_me_feed_layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
if (adView != null && adView.isLoaded())
{
adView.show();
adView.setAdListener(new AdListener() {
#Override
public void onAdClosed()
{
Intent intent = new Intent(LeftMenu.this.activity, UserFeedActivity.class);
intent.putExtra("isForUser", false);
LeftMenu.this.activity.startActivity(intent);
LeftMenu.this.activity.finish();
}
});
}
else
{
Intent intent = new Intent(LeftMenu.this.activity, UserFeedActivity.class);
intent.putExtra("isForUser", false);
LeftMenu.this.activity.startActivity(intent);
LeftMenu.this.activity.finish();
}
}
});
Which basically displays the ads if it was previously loaded.
I got two error:
03-26 20:44:37.437: E/GooglePlayServicesUtil(28151): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
Which, according to the Admob doc, is not a blocking error.
03-26 20:44:59.242: W/Ads(28151): Timed out waiting for ad response.
03-26 20:44:59.252: W/dalvikvm(28151): disableGcForExternalAlloc: false
03-26 20:44:59.257: W/Ads(28151): Failed to load ad: 2
I don't understand why I got those errors as my AdUnitId is correct.
Any idea ?
Thanks for your help.
The first error is benign and can be ignored.
The 2nd suggests that you didn't have a good network connection, or that the Admob server was under extreme load (unlikely). Make sure you have a good net connection and try again.
Related
I'm implementing google native ads on android.Most of the code I copy/pasted from this example
The only difference is I request bunch of ads instead of one ad.Here is load function:
public void loadAds(String unitId,int count) {
if (nativeAds.size()>0) {
for(UnifiedNativeAd ad: nativeAds)
ad.destroy();
}
nativeAds =new ArrayList();
//load
AdLoader.Builder builder = new AdLoader.Builder(context, unitId);
builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
// OnUnifiedNativeAdLoadedListener implementation.
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd nativeAd) {
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
//nativeAd = unifiedNativeAd;
int numAds =nativeAds.size();
int insertTo=numAds==0? firstAdPosition : firstAdPosition +numAds*(AdStep +1);
CategoryData item = new CategoryData();
item.type = ItemData.TYPE_CATEGORIES_NATIVE_AD;
if (insertTo<data.size())
data.add(insertTo, item);
item = new CategoryData();
item.type = ItemData.TYPE_CATEGORIES_SEPARATOR;
if (insertTo<data.size())
data.add(insertTo+1, item);
nativeAds.add(nativeAd);
Log.d("nativead","got ad body "+nativeAds.size()+":"+nativeAd.getBody());
notifyItemRangeInserted(insertTo,2);
}
});
VideoOptions videoOptions = new VideoOptions.Builder()
.setStartMuted(true)
.build();
NativeAdOptions adOptions = new NativeAdOptions.Builder()
.setVideoOptions(videoOptions)
.build();
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Log.d("nativead","error code " +errorCode);
/*
Toast.makeText(context, "Failed to load native ad: "
+ errorCode, Toast.LENGTH_SHORT).show();
*/
}
}).build();
AdRequest adRequest;
// get unpersonalized ads if needed
if(AppModel.getAppData().gdpr.eea && AppModel.getAppData().gdpr.consentStatus!= ConsentStatus.PERSONALIZED) {
Bundle extras = new Bundle();
extras.putString("npa", "1");
adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class,extras).build();
} else {
adRequest = new AdRequest.Builder().build();
}
adLoader.loadAds(adRequest,count);
//adLoader.loadAd(adRequest);
}
First time it loads ok, but if I restart the app it throws a bunch of errors:
I/Ads: Received log message: The ad request returned a no fill for the particular slot. The error log below that says 'Malformed native JSON response' is a benign warning that will be removed in a future SDK release.
Also error code 0 is passed into onAdFailedToLoad.If I clear app's data banners are loaded again but again only for single run
As you mentioned, in the comment above, you're testing it on real ad units (which is quite dangerous as you might get blocked for that).
You're getting NO_FILL, which means there is no inventory left to serve in the AdMob backend.
You can find out more about this issue (not an issue, we can call it a "thing") here.
To get proper results, always test with test ads, so that you get maximum fill rate for the ads while testing.
You'll find the ad unit ID's for test ads on this page.
And, if you still get NO_FILL error a lot in production, try using mediation to get ads from multiple ad sources. You'll never get 100% fill rate for your ads, though, so make sure you code accordingly to handle the NO_FILL.
Also, one more reminder, do not use live ads to test the app, you might get blocked by Admob/Google for doing that.
As the Documentation of Mopub say's here ,integrated the Native ads and followed by Native Video ads.
After integrating the code the ad request response is calling the callback method onNativeFail()
with some of the response
Below code is related to the workflow and logcat message
public class MainActivity extends Activity {
private MoPubView moPubView;
//private MoPubInterstitial mInterstitial;
private MoPubNative moPubNative;
private MoPubNativeNetworkListener moPubNativeNetworkListener;
private NativeAd.MoPubNativeEventListener moPubNativeEventListener;
AdapterHelper adapterHelper;
private NativeFullScreenVideoView nativeFullScreenVideoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
moPubNativeNetworkListener = new MoPubNativeNetworkListener() {
#Override
public void onNativeLoad(NativeAd nativeAd) {
Log.d("MoPub", "Native ad has loaded.");
}
#Override
public void onNativeFail(NativeErrorCode errorCode) {
Log.d("MoPub", "Native ad failed to load with error: " + errorCode.toString());
}
};
moPubNativeEventListener = new NativeAd.MoPubNativeEventListener() {
#Override
public void onImpression(View view) {
Log.d("MoPub", "Native ad recorded an impression.");
// Impress is recorded - do what is needed AFTER the ad is visibly shown here.
}
#Override
public void onClick(View view) {
Log.d("MoPub", "Native ad recorded a click.");
// Click tracking.
}
};
moPubNative = new MoPubNative(this, "02a2d288d2674ad09f3241d46a44356e ", moPubNativeNetworkListener);
ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_list_item)
.mainImageId(R.id.native_main_image)
.iconImageId(R.id.native_icon_image)
.titleId(R.id.native_title)
.textId(R.id.native_text)
.privacyInformationIconImageId(R.id.native_privacy_information_icon_image)
.build();
MediaViewBinder mediaViewBinder = new MediaViewBinder.Builder(R.layout.native_video_ad_layout)
.mediaLayoutId(R.id.native_ad_video_view)
.iconImageId(R.id.native_ad_icon_image)
.titleId(R.id.native_ad_title)
.textId(R.id.native_ad_text)
.build();
MoPubVideoNativeAdRenderer moPubVideoNativeAdRenderer = new MoPubVideoNativeAdRenderer(mediaViewBinder);
moPubNative.registerAdRenderer(moPubVideoNativeAdRenderer);
MoPubStaticNativeAdRenderer moPubStaticNativeAdRenderer = new MoPubStaticNativeAdRenderer(viewBinder);
moPubNative.registerAdRenderer(moPubStaticNativeAdRenderer);
EnumSet<RequestParameters.NativeAdAsset> desiredAssets = EnumSet.of(
RequestParameters.NativeAdAsset.TITLE,
RequestParameters.NativeAdAsset.TEXT,
RequestParameters.NativeAdAsset.CALL_TO_ACTION_TEXT,
RequestParameters.NativeAdAsset.MAIN_IMAGE,
RequestParameters.NativeAdAsset.ICON_IMAGE,
RequestParameters.NativeAdAsset.STAR_RATING
);
RequestParameters mRequestParameters = new RequestParameters.Builder()
.desiredAssets(desiredAssets)
.build();
moPubNative.makeRequest();
}
}
After running this code, the ad is not getting loaded and the response of my code is below
06-06 17:01:41.797 24421-24421/? I/Ads: Webview loading for native ads.
06-06 17:01:41.911 24421-24421/? I/Ads: Javascript has loaded for native ads.
06-06 17:02:18.623 24421-24421/? I/Ads: Webview loading for native ads.
06-06 17:02:18.954 24421-24421/? I/Ads: Javascript has loaded for native ads.
06-06 17:02:51.796 13278-13278/com.fabgrad.students.android D/MoPub: Native ad request failed.
com.mopub.network.MoPubNetworkError: No ads found for ad unit.
at com.mopub.network.AdRequest.parseNetworkResponse(AdRequest.java:180)
at com.mopub.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:132)
at com.mopub.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)
06-06 17:02:51.800 13278-13278/com.fabgrad.students.android D/MoPub: Native ad failed to load with error: Server returned empty response.
As per their blog, No ads found will come for following scenarios:
These errors indicate that there was no fill for your ad unit.
No ad network will have fill 100% of the time, so seeing this log is normal. New apps and apps with very low volume tend to experience lower fill rates. Contact your ad network representative if you have any concerns related to a particular network’s fill.
If you are consistently seeing no fill, review your ad network placement details in the Networks tab of the MoPub UI, as well as your settings in the Network’s UI.
You won’t be able to show ads from Certified Ad Networks if you forgot to include the network SDK or adapter files. The Couldn't locate or instantiate custom event and Unable to find Native Network or Custom Event adapter log messages are indicators that the network SDK or adapter file is missing, renamed, or in the wrong location.
Review our Integrating Third-Party Ad Networks documentation to resolve this.
Link for reference.
I am trying to put an interstitial ad after every 5 games the user plays. What I've done is created an empty object with the following script:
using UnityEngine;
using GoogleMobileAds.Api;
public class AdBetweenGames : MonoBehaviour {
InterstitialAd interstitial;
void Start()
{
if(PlayerPrefs.GetInt("games-played", -1) % 5 == 0)
{
this.ShowInterstitial();
}
}
private void RequestInterstitial()
{
string adUnitId = "UNITID";
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitId);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
}
public void ShowInterstitial()
{
RequestInterstitial();
Debug.Log("Interstatial ad called.");
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
}
I am sure the code here is called properly based on the debug log. I've also tried doing banner ads and they show without a problem after 1-2 seconds. I am making an android game.
Also I was thinking, could it be that the ad needs some extra time to load and I am clicking try again too early. But then what would a solution be?
The interstitial ad of ad mob need time to load try it in update and wait for at least 4 sec and call ShowInterstitial function inside a bool set its value true in start and false it after calling the function to show ad once.
I am trying to implement Amazon Ads in my Android app, but have not been able to get any ads to display. The message in my logs is always:
D/AmazonMobileAds DefaultAdListener: Default ad listener called - Ad Failed to Load. Error code: NO_FILL, Error Message: Server Message: no results
I have set up my account, verified my tax information, I have an application key. I have tried both with testing enable and disabled with the same results. I have followed the guide for loading ads from both Amazon and Admob for backfill, but the AdMob ads are then not retrieved either. If I retrieve just AdMob ads, I have no problem with them. The relevant code is below:
in onCreate():
AdRegistration.setAppKey(getString(R.string.amazon_ad_id));
AdRegistration.enableTesting(true);
AdRegistration.enableLogging(true);
// Initialize ad views
amazonAdView = new com.amazon.device.ads.AdLayout(this);
amazonAdView.setListener(new MyAdListener());
admobAdView = new com.google.android.gms.ads.AdView(this);
admobAdView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
admobAdView.setAdUnitId(getString(R.string.banner_ad_unit_id));
Method called to retrieve an ad:
void getAd(){
// Initialize view container
adViewContainer = (ViewGroup)findViewById(R.id.adView);
amazonAdEnabled = true;
adViewContainer.removeAllViews();
adViewContainer.addView(amazonAdView);
AdTargetingOptions adOptions = new AdTargetingOptions();
adOptions.enableGeoLocation(true);
amazonAdView.loadAd(adOptions);
}
Inner AdListener class
class MyAdListener extends DefaultAdListener {
public void onAdLoaded(com.amazon.device.ads.AdLayout view, AdProperties adProperties) {
Log.d(LOG_TAG, "Ad Loaded");
if (!amazonAdEnabled) {
amazonAdEnabled = true;
adViewContainer.removeView(admobAdView);
adViewContainer.addView(amazonAdView);
}
}
public void onAdFailedToLoad(com.amazon.device.ads.AdLayout view, AdError error) {
Log.d(LOG_TAG, "Ad Failed to load");
// Call AdMob SDK for backfill
if (amazonAdEnabled) {
amazonAdEnabled = false;
adViewContainer.removeView(amazonAdView);
adViewContainer.addView(admobAdView);
}
admobAdView.loadAd((new com.google.android.gms.ads.AdRequest.Builder()).build());
}
}
In my logging, neither of the statements from the AdListener shows, so it appears that the listener is never being called. So, I have two issues here:
Why am I always receiving a NO_FILL response, even when testing?
Why is the listener never called?
When admob code is initialized in a Phonegap/Cordova app that makes use of localStorage, the localStorage seems to get overridden by the admob ad view.
Here's the code for initialization of the admob code (using v6.2.1 of the SDK) in a Cordova 2.2 app::
public class Foo extends DroidGap
{
private AdView adView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.splash);
super.loadUrl("file:///android_asset/www/index.html", 3000);
adView = new AdView(this, AdSize.SMART_BANNER, "AD_MOB_ID");
root.addView(adView);
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
The ad gets loaded but the localStorage which I use to maintain the session of the user seems to get cleared and the login page is displayed. When I comment the admob initialization code, the localStorage gets reverted and the user's session is back.
As per the instructions on [1] and [2], I have added a handler::
public class Foo extends DroidGap
{
private AdView adView;
private Handler mHandler = new Handler();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.splash);
super.loadUrl("file:///android_asset/www/index.html", 3000);
mHandler.postDelayed(new Runnable() {
public void run() {
loadAdmob();
}
}
}
private void loadAdmob()
{
adView = new AdView(this, AdSize.SMART_BANNER, "AD_MOB_ID");
root.addView(adView);
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
This tries loading the ads 10 seconds later but I get the following message in logcat::
adRequestUrlHtml: <html>...
Received ad url: <url: ...
Request Scenario: Offline with no buffered ads.
Network is unavailable. Aborting ad request.
onFailedToReceiveAd(A network error occurred.)
I can confirm that there is no problem with the network connection (because the rest of the data in the app gets displayed and is fetched over the network) and yet the ad does not display. Am I doing something wrong or is this a bug? A workaround for this problem will be helpful.
[1] Android + HTML5(LocalStorage) + Admob: Bug?
[2] https://github.com/phonegap/phonegap/wiki/In-App-Advertisements
I experienced the same problem with receiving the network error when introducing the delay (perhaps due to running on another thread, it loses the necessary permissions to query the network.)
The way I solved this was to override onAttachToWindow() and invoke the adMob initialization from in there, and removing the handler code altogether. It has the side effect of being near instant with no delay as well.
#Override public void onAttachedToWindow() {
super.onAttachedToWindow();
doAdMob();
};
private void doAdMob() {
adView = new AdView(this, AdSize.BANNER, "ID");
LinearLayout layout = super.root;
layout.addView(adView);
AdRequest request = new AdRequest();
request.addTestDevice("ID");
adView.loadAd(request);
}
I'm using Cordova 2.5 btw.
UPDATE: AdMod still corrupts the localstorage, but it's loaded by the time AdMob has a chance to wipe it out. However, on a subsequent reload, it will be reset. I've since resolved this behavior by adding
window.setTimeout(saveSettings, 5000);
after I've loaded my localStorage settings. This is highly hackish of course.. but seems to do the trick so far. Also note, I've only been using the test ads for AdMob.