Should we first call MobileAds.setRequestConfiguration or MobileAds.initialize? - android

There isn't much documentation on this. I was wondering, should we first call
RequestConfiguration conf= new RequestConfiguration.Builder()
.setMaxAdContentRating(
MAX_AD_CONTENT_RATING_T)
.build();
MobileAds.setRequestConfiguration(conf);
MobileAds.initialize(context, APP_ID);
Or
MobileAds.initialize(context, APP_ID);
RequestConfiguration conf= new RequestConfiguration.Builder()
.setMaxAdContentRating(
MAX_AD_CONTENT_RATING_T)
.build();
MobileAds.setRequestConfiguration(conf);
In https://developers.google.com/admob/android/quick-start
Although Google recommend calling MobileAds.initialize as early as possible
Before loading ads, have your app initialize the Mobile Ads SDK by
calling MobileAds.initialize() which initializes the SDK and calls
back a completion listener once initialization is complete (or after a
30-second timeout). This needs to be done only once, ideally at app
launch.
They also mention "request-specific flags" need to be set before MobileAds.initialize.
Warning: Ads may be preloaded by the Mobile Ads SDK or mediation
partner SDKs upon calling MobileAds.initialize(). If you need to
obtain consent from users in the European Economic Area (EEA), set any
request-specific flags (such as tagForChildDirectedTreatment or
tag_for_under_age_of_consent), or otherwise take action before loading
ads, ensure you do so before initializing the Mobile Ads SDK.
So, not super clear on which should be called first.

According to Google Developer support, the following is the right way to do
https://groups.google.com/forum/#!category-topic/google-admob-ads-sdk/android/17oVu0sABjs
RequestConfiguration conf= new RequestConfiguration.Builder()
.setMaxAdContentRating(
MAX_AD_CONTENT_RATING_T)
.build();
MobileAds.setRequestConfiguration(conf);
MobileAds.initialize(context, APP_ID);

It's done as such:
MobileAds.RequestConfiguration =
new RequestConfiguration
.Builder()
.SetTagForChildDirectedTreatment(RequestConfiguration.TagForChildDirectedTreatmentTrue)
.SetMaxAdContentRating(RequestConfiguration.MaxAdContentRatingG)
#if DEBUG
.SetTestDeviceIds(new[] { "..." })
#endif
.Build();

according to the official documentation
Before loading ads, have your app initialize the Mobile Ads SDK by calling MobileAds.initialize() which initializes the SDK and calls back a completion listener once initialization is complete (or after a 30-second timeout). This needs to be done only once, ideally at app launch.
So , you should initialize MobileAds first ,look at the example here form the official documentation :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}

Related

Initialization functionality unavailable for this session, in google Ads

I'm getting this error of google Ads, suddently. I can't find anything here with this error.
Notice that ads are displaying correctly
Google Mobile Ads SDK initialization functionality unavailable for this session. Ad requests can be made at any time.
Have you tried restarting your test device? It worked for me. Also if you have other apps with admob try uninstalling them since numerous calls will be made leading to the problem you are experiencing.
In my case, I solved this error by adding the following lines to my onCreate method:
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

Can I request new ad from onAdFailedToLoad?

During testing of my application, I noticed that I very frequently get ERROR_CODE_NO_FILL for Interstitial Ad. I think that the same situation with real ads might be one of the causes of low income from the application, so I want to increase the rate of showed ads. As the solution, I decided to make requests until I will get some app, so it might take from 5 to 50 requests until I will get out of onAdFailedToLoad. Is it a legitimate way to do this? Won't I get banned by AdMob with such way of getting more ads?
That's what I do in setAdListener in onCreate:
#Override
public void onAdFailedToLoad(int errorCode) {
requestNewInterstitial();
}
And requestNewInterstitial():
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.build();
mInterstitialAd.loadAd(adRequest);
}
No, you won't get banned...
You can do this, but don't show the Ad as soon it is loaded, this will be a bombarding of Ads & user won't like that...

High ram usage with admob

I am trying to place ads inside my app. According to Admob Documentation I have to initialize Mobile Ads SDK
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
This causes spike in high ram usage in code.
But if i remove this line then ram usage drops & and this line of code doesn't seem to have any affect on servering ads inside the app.
Also when requesting ad from admob ram usage again spike up and causes 3-4 GC events on app startup. I believe this is memory leak.
Here's how i am requesting ad in onCreate method
AdRequest request = null;
if (BuildConfig.DEBUG) {
//Facebook Audience Network
List<String> testDeviceId = new ArrayList<>();
testDeviceId.add("TESTID");//Redmi Note 3
testDeviceId.add("TESTID");//Moto G 1st Gen
AdSettings.addTestDevices(testDeviceId);
//Google Ad-mob
request = new AdRequest.Builder()
.addTestDevice("TESTID")//Redmi Note 3
.addTestDevice("TESTID")//Mot G 1st Gen
.build();
} else {
request = new AdRequest.Builder()
.build();
}
AdView mAdView = findViewById(R.id.adView);
mAdView.loadAd(request);
When loading this banner ads several GC event are kicked in. If i don't load ads GC event are never kicked in.
Is this behavior normal with admob? How can i resolve this?
Google AdView has WebView with a lot of animation inside. It will heats up all mobile CPU.
AdView take 30% of CPU.
Solution : You can also add custom listeners to destroy after some time and recreate in order to handle it even better. Serverside there is also a parameter telling the app ad how soon should ask for a new ad, I am not sure if it exist in all cases but it is there for DFP accounts.
here is the easiest way i would suggest
new Handler(new Handler.Callback() {
#Override
public boolean handleMessage(Message msg) {
if (!isBeingDestroyed) {
final AdRequest adRequest = new AdRequest();
final AdView adView = (AdView) findViewById(R.id.ad);
adView.loadAd(adRequest);
}
}).sendEmptyMessageDelayed(0, 1000);
Here is the link that provide complete solution for this.
Hope it will help you.
Yes, that behaviour is normal. AdView is a dynamic WebView which consumes about 50mb RAM. Most of the memory leaks occur when you rotate screen and instance of previous Activity is attached to an element like listener or thread. Here are some examples. To check whether your app leaks or not you can use LeakCanary or Android Studio.
To check leaks in Android Studio
Start Memory Profiler
Select Memory and "Dump Java Heap"
Export file as .hprof file
Drag .hprof file to Android Studio and look for Analyzer Tasks and push run button to check if your activity leaks.
Your app is still within an acceptable limit of RAM usage for most devices.
You can put android:largeHeap="true" in your AndroidManifest.xml file, so that your users won't get affected.
You are absolutely right. MobileAds.initilize() can be removed, and the application will work, BUT. When you request the first load of ads, the initilize() method will be called under the hood.
I recommend that you call this method as soon as possible. For example, in your Application class. Also, starting from version Admob 21.0.0, you can optimize the initialization process. Just add the following lines to your manifest
<meta-data
android:name="com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION"
android:value="true"/>
<meta-data
android:name="com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"
android:value="true"/>
i dont know if this will help but if you care about ram usage and you use ads in many activities you could start the ad mob from application class and that case the ad will only initialized once

Admob banner shows only on first launch

After following the official Admob guide for Unity, I successfully got a banner to show up on my game. The problem is, it only shows up the very first time I build the game. If I quit the game and launch it again, it doesn't show. Deleting the game and building from Unity again is the only thing that shows banners.
My code is as follows:
void Start() {
RequestBanner();
}
private void RequestBanner() {
string id = "ca-app-pub-numbers...";
view = new BannerView(id, AdSize.SmartBanner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
view.LoadAd(request);
}
I have tried calling view.Destroy() on application quit or in a coroutine, as well as view.Show() and view.Hide() at various locations with no luck.
How can I make admob show banner consistently?

Custom ads on admob android studio based on keyword

I have searched online but had not luck so far in my research, I have set up admobs in my android app and now I only want relevant ads to show up based on keyword that I set. My app is based on toys, so I only want ads related to toys to show
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
Looking at this: Admob ad on custom Dialog
This person has used:
AdRequest request = new AdRequest();
Set<String> keywords = new HashSet<String>();
keywords.add("game");
request.setKeywords(keywords);
However, this does not work.
I think you need this:
AdRequest request = new AdRequest.Builder()
.addKeyword("game").build();
.adView.loadAd(request);
You can try with this, but here are some examples, maybe you find what you allso need:
1.Test ads
Set up test ads by passing your hashed Device ID to AdRequest.Builder.addTestDevice:
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
.addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") // An example device ID
.build();
2.Location
Location targeting information may also be specified in the AdRequest:
AdRequest request = new AdRequest.Builder()
.setLocation(location)
.build();
3.Gender
If your app already knows a user's gender, it can provide that information in the ad request for targeting purposes. The information is also forwarded to ad network mediation adapters if mediation is enabled.
AdRequest request = new AdRequest.Builder()
.setGender(AdRequest.GENDER_FEMALE)
.build();
4.Birthday
If your app already knows a user's birthday, it can provide that information in the ad request for targeting purposes. This information is also forwarded to ad network mediation adapters if mediation is enabled.
AdRequest request = new AdRequest.Builder()
.setBirthday(new GregorianCalendar(1985, 1, 1).getTime())
.build();
5.Designed for Families setting
If you have opted your app in to Google Play's Designed for Families program and you show ads in your app, you need to ensure those ads comply with the Designed for Families program requirements and ad policies.
Ad requests can be tagged as designed for families by setting the is_designed_for_families parameter to true in the extras:
Bundle extras = new Bundle();
extras.putBoolean("is_designed_for_families", true);
AdRequest request = new AdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
.build();
6.Child-directed setting
For purposes of the Children's Online Privacy Protection Act (COPPA), there is a setting called "tag for child directed treatment".
As an app developer, you can indicate whether you want Google to treat your content as child-directed when you make an ad request. If you indicate that you want Google to treat your content as child-directed, we will take steps to disable IBA and remarketing ads on that ad request. The setting can be used with all versions of the Google Play services SDK, via AdRequest.Builder.tagForChildDirectedTreatment(boolean):
If you set tagForChildDirectedTreatment to true, you will indicate that your content should be treated as child-directed for purposes of COPPA.
If you set tagForChildDirectedTreatment to false, you will indicate that your content should not be treated as child-directed for purposes of COPPA.
If you do not set tagForChildDirectedTreatment, ad requests will include no indication of how you would like your content treated with respect to COPPA.
AdRequest request = new AdRequest.Builder() .tagForChildDirectedTreatment(true) .build();
By setting this tag, you certify that this notification is accurate and you are authorized to act on behalf of the owner of the app. You understand that abuse of this setting may result in termination of your Google account.
7.Keyword
Add a keyword for targeting purposes.
AdRequest request = new AdRequest.Builder() .addKeyword(someKeyword) .build();
Loading an ad with targeting
Once your request targeting information is set, call loadAd on the AdView with your AdRequest instance.
AdRequest request = new AdRequest.Builder()
.setLocation(location)
.setGender(AdRequest.GENDER_FEMALE)
.setBirthday(new GregorianCalendar(1985, 1, 1).getTime())
.tagForChildDirectedTreatment(true)
.addKeyword("game")
.build();
adView.loadAd(request);
Additional information can be found on this link.
After doing some research, it does not look like keywords are supported.
There have been various questions over the years that all have the same answer:
The documentation for targeting ads does not include any information on using keywords.
You can add keywords to your AdRequest using addKeyword(String keyword) in AdRequest.Builder, which is documented here. An example call would be like below:
AdRequest adRequest = new AdRequest.Builder().addKeyword("game").build();
adView.loadAd(adRequest);
However, as stated above- it does not look like these keywords are being used on the backend.
The AdMob support site shows that there are three ways advertisers can choose to target their ads- context (keywords, presumably implicit rather than explicit), placement, and interest.
You can filter outside of your app by allowing and blocking ads, which if you truly do only want to show ads related to toys, then you will have to block everything except the toys category shown in the general category list.
You can try doing this :
AdRequest.Builder builder = new AdRequest.Builder()
.addKeyword("game");
Now you can build the adrequest.
As per the documentation to add a keyword for targeting purposes.
You can use this single line code.
public AdRequest.Builder addKeyword (String keyword)

Categories

Resources