Localstorage overwritten with Cordova plus admob - android

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.

Related

google loads native ad banner only once

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.

Not able to implement Native Video ads through Manual Integration in android using Mopub

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.

Reduce android app startup time

I am developing an app that uses a shared preferences file and it also contains ads. When I open my app for the first time (running from android studio) my main activity's taking 14-16 seconds to load. After caching it takes 2 seconds to load. I realised that I was putting too many operations in my onCreate() method.Then I tried using onResume(), but it is still taking the same time. I would like to know how I could reduce this startup time. My app uses a shared preference file and it contains ads.I also noticed that my app cache is 20MB.
Code is as follows
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
EditText nme_key = (EditText)findViewById(R.id.name_key);
EditText cls_key = (EditText)findViewById(R.id.class_key);
EditText num_key = (EditText)findViewById(R.id.number_key);
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.preference_file_key),MODE_PRIVATE);
nme_key.setText(sharedPreferences.getString("name","name"));
cls_key.setText(sharedPreferences.getString("class","class"));
num_key.setText(sharedPreferences.getString("number","number"));
MobileAds.initialize(getApplicationContext(), "");
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice("").build();
mAdView.loadAd(adRequest);
}
I have 3 questions
How to reduce the startup time of my app ( Threads?)
How to reduce the cache size of my app
How can I improve my app performance
I found out that Ads are the reason that makes my application takes too long to start.
So, this how I fixed it:
//run in the background
AsyncTask.execute(() -> {
MobileAds.initialize(this, initializationStatus -> {});
//Banner Ad
AdView adView = findViewById(R.id.ad_view);
AdRequest adRequest = new AdRequest.Builder().build();
//Interstitial Ad
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
//you have to load the Ads in the foreground
this.runOnUiThread(() -> {
adView.loadAd(adRequest);
mInterstitialAd.loadAd(new AdRequest.Builder().build());
});
});
How to show Interstitial Ad:
button.setOnClickListener(view -> {
//check first, if it is instantiated and loaded
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
});
But be aware that this will make your app start without showing any ads until they load up.
I know I'm very late but I hope this will help people in the feature.

Android AdMob Timed out

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.

Error receiving Ad when using AdMob SDK

I have created a application with webview for displaying for AdMob. I am using the following code.
private AdView adView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout1);
adView = new AdView(this, AdSize.BANNER, "/6253334/dfp_example_ad");
View view = addHome(this,"https://www.google.co.in/");
layout.addView(view,new LinearLayout.LayoutParams(200, 100));
final AdRequest adRequest = new AdRequest();
adRequest.addTestDevice("1CD0A829B8C49C9F7590DD3B4C5EC553");
adRequest.setTesting(true);
setContentView(layout);
new Thread(){
public void run() {
Looper.prepare();
adView.loadAd(adRequest);
};
}.start();
}
I am always getting onFailedToReceiveAd(A network error occurred) error, instead of getting the ads.
Have you added your INTERNET Permission to your Manifest file and made sure the device actually has Internet access?
<uses-permission android:name="android.permission.INTERNET" />
Are other applications on your phone able to access the Internet? What I mean is have you tested if other apps are able to access the internet? Maybe this is not an issue of your application, but of the phone in general being unable to access the internet for whatever reason.
Furthermore, what I see in your above code:
You are calling "layout = findViewById(...)" before even calling "setContentView(...)". This should actually result in a Nullpointer Exception when calling layout.addView().
You could also try this code (inside your onCreate() method):
setContentView(R.layout.yourlayout);
AdView ad = new AdView(this, AdSize.SMART_BANNER, "yourid");
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout1);
if (ll != null) {
ll.addView(ad);
}
ad.loadAd(new AdRequest());

Categories

Resources