HMS Core Advertising Kit (Ads Kit) provides developers with traffic monetization services and advertising identification services. Relying on Huawei's terminal capabilities, it integrates resources and helps developers obtain high-quality advertising content. At the same time, it provides conversion tracking parameter services to support third-party monitoring platforms and advertisers for conversion attribution analysis. Below, we share some problems that developers often encounter when accessing advertising services , hoping to provide reference for developers who encounter similar problems.
1. What should I do if the banner ad size becomes larger when the mobile phone is converted from portrait to landscape?
solution:
Fixed BannerView width or height. For example, the height of the Banner ad is fixed in the following code:
<com.huawei.hms.ads.banner.BannerView
android:id="@+id/hw_banner_view"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
The normal display effect of the banner advertisement is as follows:
2. When using the sample code to test, what should I do if the native ad does not display the "Why show this ad" icon "i" and the "Don't show this ad again" icon "x" (that is, the ad close button)?
Cause Analysis:
In order to meet the high requirements of local compliance, the SDK has added the ability to "do not show this ad again", and display the icon "x" of "do not show this ad again" by default. This icon is only displayed on devices released in non-mainland China regions, so it will not be visible when testing on devices released in mainland China.
Reference suggestion:
If the App is only launched in the non-mainland China region, it will not affect the use. You can use the device issued in the non-mainland China region to check the display effect.
If the app is only launched in mainland China, and the close button needs to be used, it is recommended to implement it in the native advertising layout.
If the app is launched globally, while implementing the close button by yourself, you also need to pay attention to whether the app will conflict with the close button displayed by default after the app is launched in non-mainland China.
Replenish:
The function of "do not show this ad again" is set in the native ad setting option NativeAdConfiguration.Builder , which provides setRequestCustomDislikeThisAd()
method to set whether to customize the function of "do not show this ad again", the default is false. The form of expression is as follows:
If set to true, the "Why I saw this ad" icon "i" will be displayed, as shown in the image below:
When implementing the close button by yourself, it is recommended to set it to true in order to avoid two close buttons appearing in applications that are not launched in mainland China.
Second, you can set the icon display position by calling the NativeAdConfiguration.Builder().setChoicesPosition(int choicesPosition)
method.
Ad option placement:
3. How do I request multiple native ads at once?
Native ads provide the method loadAds() to request multiple native ads at the same time.
The loadAds() request has two parameters, in addition to AdParam , maxAdsNum is the maximum number of ads loaded by the request, and the maximum value is 5. The number of ads returned by the SDK will be less than or equal to the requested number of ads, and the returned ads will not be the same. The sample code looks like this:
nativeAdLoader.loadAds(new AdParam.Builder().build(), 5);
When the ad is loaded successfully, the SDK will call the onNativeAdLoaded() method of the NativeAd.NativeAdLoadedListener listener multiple times to return a NativeAd object, and call the onAdLoaded method of the AdListener listener after the ads are returned to notify the success of the request; when the ad is loaded After failure, the SDK will call the onAdFailed() method of the AdListener listener.
The sample code is shown below, in which testy63txaom86" is the ad slot ID dedicated to testing, and it needs to be changed to the official ad slot ID when the App is officially released.
NativeAdLoader.Builder builder = new NativeAdLoader.Builder(this, "testy63txaom86");
NativeAdLoader nativeAdLoader = builder.setNativeAdLoadedListener(new NativeAd.NativeAdLoadedListener() {
@Override
public void onNativeAdLoaded(NativeAd nativeAd) {
// 广告加载成功回调,多个广告对应多个回调
...
}
}).setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// 广告全部返回后回调,说明所有广告返回成功
...
}
@Override
public void onAdFailed(int errorCode) {
// 广告加载失败回调
...
}
}).build();
nativeAdLoader.loadAds(new AdParam.Builder().build(), 5);
Note: Before using NativeAdLoader to load ads again, make sure the previous request has completed.
4. When scrolling to play a patch ad in the app, an error message onMediaError : -1 is reported
After the first playback of the patch ad is completed, the error code -1 is returned when it is played again.
Cause Analysis:
- The current network is unavailable.
- After the patch ad is played, it is not released, resulting in -1 being returned the next time it is played.
solution:
- Check the network. To allow HTTP(S) network requests on mobile phones with targetSdkVersion 28 and above, do the following configuration in "AndroidManifest.xml".
<application
...
android:usesCleartextTraffic="true"
>
...
</application>
- Release the patch advertisement in the onMediaCompletion() callback method in InstreamMediaStateListener, which needs to be released after each playback is completed.
public void onMediaCompletion(int playTime) {
updateCountDown(playTime);
removeInstream();
playVideo();
}
private void removeInstream() {
if (null != instreamView) {
instreamView.onClose();
instreamView.destroy();
instreamContainer.removeView(instreamView);
instreamContainer.setVisibility(View.GONE);
instreamAds.clear();
}
}
5. How to test advertisement placement in non-mainland China regions in China?
With the increasing demand of domestic developers to monetize advertisements in non-mainland China, and the lack of overseas testing environments for developers, there is a situation where no advertisements can be requested when using the official advertising space ID test in China.
Workaround (App launch takes more than 12 hours):
Method 1: Use an overseas version of Huawei mobile phone, connect to a VPN network in a non-mainland China region (non-US), set the country region in the mobile phone to non-mainland China, and make sure that the app has been launched in this region for testing.
Method 2: Register as an overseas developer and use the Cloud Debugging function provided by HMS ToolKit , that is, a remote real machine for testing.
Method 3: Use the "Ad Test Service" in the "Media Service Platform" to test. For details, please see: Realization Test
Note: Huawei advertising has four delivery sites around the world: China, Asia, Africa and Latin America, Europe, and Russia. Different site advertisements do not support cross-regional requests for advertisements [If you need to request cross-site advertisements, you need to use the network of the corresponding country (you can use a vpn proxy) and the mobile device of the corresponding country]. The country details corresponding to each site can refer to the document
Learn more details>>
Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee
Follow us to know the latest technical information of HMS Core for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。