Huawei Ads Kit

Pendrush

Well-Known Member
Licensed User
Longtime User
Wrapper for Huawei Ads Kit. Based on Huawei Ads Kit v13.4.37.300 (28 December 2020). Github link.
Banner, Interstitial and Reward Ads.
Info about HUAWEI Ads Publisher Service.

HuaweiAdsKit

Author:
Author: Huawei - B4a Wrapper: Pendrush
Version: 1.30
  • HuaweiAdsKit
    • Events:
      • AdClicked
      • AdClosed
      • AdFailed (Error As String)
      • AdLeave
      • AdLoaded
      • AdOpened
    • Fields:
      • BANNER_SIZE_160_600 As Object
        Banner - 160x600, you need to set view width to 160dip and height to 600dip
      • BANNER_SIZE_300_250 As Object
        Banner - 300x250, you need to set view width to 300dip and height to 250dip
      • BANNER_SIZE_320_100 As Object
        Banner - 320x100, you need to set view width to 320dip and height to 100dip
      • BANNER_SIZE_320_50 As Object
        Banner - 320x50, you need to set view width to 320dip and height to 50dip
      • BANNER_SIZE_360_144 As Object
        Banner - 360x144, you need to set view width to 360dip and height to 144dip
      • BANNER_SIZE_360_57 As Object
        Banner - 360x57, you need to set view width to 360dip and height to 57dip
      • BANNER_SIZE_468_60 As Object
        Banner - 468x60, you need to set view width to 468dip and height to 60dip
      • BANNER_SIZE_728_90 As Object
        Banner - 728x90, you need to set view width to 728dip and height to 90dip
      • BANNER_SIZE_SMART As Object
        Banner - Smart, you need to set view width to 100%x and height to 32dip or 50dip or 90dip
    • Functions:
      • BringToFront
      • Initialize (EventName As String, AdId As String, AdSize As com.huawei.hms.ads.BannerAdSize)
        Initialize banner ad
        HuaweiAdsKitBanner1.Initialize("HuaweiAdsKitBanner1", "testw6vs28auh3", HuaweiAdsKitBanner1.BANNER_SIZE_320_50)
        Activity.AddView(HuaweiAdsKitBanner1, 50%x-160dip, 0, 320dip, 50dip)
      • Invalidate
      • Invalidate2 (arg0 As android.graphics.Rect)
      • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
      • IsInitialized As Boolean
      • LoadAd (BannerRefresh As Long, Personalized As Boolean, AdContentClassification As String, TagForUnderAgeOfPromise As Int, TagForChildProtection As Int)
        Load banner ad
        BannerRefresh - refresh time for banner ad in seconds
        Personalized - if is True personlized ad will be loaded, False for non personlized
        AdContentClassification - Sets the maximum ad content rating for the ad requests of your app. The ads obtained using this method have a content rating at or below the specified one.
        TagForUnderAgeOfPromise - Sets the tag for users under the age of consent, to comply with the General Data Protection Regulation (GDPR). Ad requests with this tag enabled will be unable to obtain personalized ads.
        TagForChildProtection - Sets the tag for child-directed content, to comply with the Children's Online Privacy Protection Act (COPPA).
        HuaweiAdsKitBanner1.LoadAd(30, True, RequestOptions1.AD_CONTENT_CLASSIFICATION_W, RequestOptions1.TAG_FOR_UNDER_AGE_PROMISE_TRUE, RequestOptions1.TAG_FOR_CHILD_PROTECTION_TRUE)
      • Pause
        Pause banner ad.
        Should be called from Activity_Pause
      • RemoveView
      • RequestFocus As Boolean
      • Resume
        Resume banner ad.
        Should be called from Activity_Resume
      • SendToBack
      • SetBackgroundImage (arg0 As android.graphics.Bitmap) As BitmapDrawable
      • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
      • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
      • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
      • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    • Properties:
      • Background As android.graphics.drawable.Drawable
      • Color As Int [write only]
      • Enabled As Boolean
      • Height As Int
      • Left As Int
      • Padding As Int()
      • Parent As Object [read only]
      • Tag As Object
      • Top As Int
      • Visible As Boolean
      • Width As Int
  • HuaweiAdsKitConsent
    • Events:
      • ConsentStatusFailed (Error As String)
      • IsNeedConsent (AdProviders As Map)
      • NeedConsent (ConsentStatus As Int)
      • UpdateConsentStatus (ConsentStatus As Int)
    • Fields:
      • CONSENT_STATUS_NON_PERSONALIZED As Object
      • CONSENT_STATUS_PERSONALIZED As Object
      • CONSENT_STATUS_UNKNOWN As Object
      • DEBUG_DISABLED As Object
      • DEBUG_NEED_CONSENT As Object
      • DEBUG_NOT_NEED_CONSENT As Object
    • Functions:
      • CheckConsentStatus (DebugMode As Boolean, DebugConsent As com.huawei.hms.ads.consent.constant.DebugNeedConsent, DebugConsentStatus As com.huawei.hms.ads.consent.constant.ConsentStatus)
        Check consent status.
        DebugMode - if is True second and third parameters will be applied, if it is False, second and third parameters will be ignored.
        HuaweiAdsKitConsent1.CheckConsentStatus(True, HuaweiAdsKitConsent1.DEBUG_NEED_CONSENT, HuaweiAdsKitConsent1.CONSENT_STATUS_UNKNOWN)
      • Initialize (EventName As String)
        Initialize HuaweiAdsConsent
        HuaweiAdsKitConsent1.Initialize("HuaweiAdsKitConsent1")
      • IsInitialized As Boolean
      • SetConsentStatus (consentStatus As com.huawei.hms.ads.consent.constant.ConsentStatus)
        Set consent status.
        HuaweiAdsKitConsent1.SetConsentStatus(HuaweiAdsKitConsent1.CONSENT_STATUS_PERSONALIZED)
  • HuaweiAdsKitInterstitial
    • Events:
      • AdClicked
      • AdClosed
      • AdFailed (Error As String)
      • AdImpression
      • AdLeave
      • AdLoaded
      • AdOpened
    • Functions:
      • Initialize (EventName As String, AdId As String)
        Initialize interstitial ad
        HuaweiAdsKitInterstitial1.Initialize("HuaweiAdsKitInterstitial1", "teste9ih9j0rc3")
        Test AdId for Image Ad = teste9ih9j0rc3
        Test AdId for Video Ad = testb4znbuh3n2
      • IsInitialized As Boolean
      • IsLoaded As Boolean
        Check is interstitial ad loaded
      • LoadAd (Personalized As Boolean, AdContentClassification As String, TagForUnderAgeOfPromise As Int, TagForChildProtection As Int)
        Load interstitial ad
        Personalized - if is True personlized ad will be loaded, False for non personlized
        AdContentClassification - Sets the maximum ad content rating for the ad requests of your app. The ads obtained using this method have a content rating at or below the specified one.
        TagForUnderAgeOfPromise - Sets the tag for users under the age of consent, to comply with the General Data Protection Regulation (GDPR). Ad requests with this tag enabled will be unable to obtain personalized ads.
        TagForChildProtection - Sets the tag for child-directed content, to comply with the Children's Online Privacy Protection Act (COPPA).
        HuaweiAdsKitInterstitial1.LoadAd(True, RequestOptions1.AD_CONTENT_CLASSIFICATION_W, RequestOptions1.TAG_FOR_UNDER_AGE_PROMISE_TRUE, RequestOptions1.TAG_FOR_CHILD_PROTECTION_TRUE)
      • ShowAd
        Show interstitial ad
  • HuaweiAdsKitRequestOptions
    • Fields:
      • AD_CONTENT_CLASSIFICATION_A As String
        Sets the maximum ad content rating for the ad requests of your app. The ads obtained using this method have a content rating at or below the specified one.
        AD_CONTENT_CLASSIFICATION_A: content suitable only for adults.
      • AD_CONTENT_CLASSIFICATION_J As String
        Sets the maximum ad content rating for the ad requests of your app. The ads obtained using this method have a content rating at or below the specified one.
        AD_CONTENT_CLASSIFICATION_J: content suitable for junior and older audiences.
      • AD_CONTENT_CLASSIFICATION_PI As String
        Sets the maximum ad content rating for the ad requests of your app. The ads obtained using this method have a content rating at or below the specified one.
        AD_CONTENT_CLASSIFICATION_PI: content suitable for audiences with parental guidance.
      • AD_CONTENT_CLASSIFICATION_UNKOWN As String
        Sets the maximum ad content rating for the ad requests of your app. The ads obtained using this method have a content rating at or below the specified one.
        AD_CONTENT_CLASSIFICATION_UNKOWN: Unknown
      • AD_CONTENT_CLASSIFICATION_W As String
        Sets the maximum ad content rating for the ad requests of your app. The ads obtained using this method have a content rating at or below the specified one.
        AD_CONTENT_CLASSIFICATION_W: content suitable for widespread audiences.
      • TAG_FOR_CHILD_PROTECTION_FALSE As Int
        Sets the tag for child-directed content, to comply with the Children's Online Privacy Protection Act (COPPA).
        TAG_FOR_CHILD_PROTECTION_FALSE: You do not want your ad content to be COPPA-compliant.
      • TAG_FOR_CHILD_PROTECTION_TRUE As Int
        Sets the tag for child-directed content, to comply with the Children's Online Privacy Protection Act (COPPA).
        TAG_FOR_CHILD_PROTECTION_TRUE: You want your ad content to be COPPA-compliant (interest-based ads and remarketing ads will be disabled for the ad request).
      • TAG_FOR_CHILD_PROTECTION_UNSPECIFIED As Int
        Sets the tag for child-directed content, to comply with the Children's Online Privacy Protection Act (COPPA).
        TAG_FOR_CHILD_PROTECTION_UNSPECIFIED: You do not want to indicate whether your ad content needs to be COPPA-compliant.
      • TAG_FOR_UNDER_AGE_PROMISE_FALSE As Int
        Sets the tag for users under the age of consent, to comply with the General Data Protection Regulation (GDPR). Ad requests with this tag enabled will be unable to obtain personalized ads.
        UNDER_AGE_PROMISE_FALSE: You do not want the ad content to meet the ad standard for users under the age of consent.
      • TAG_FOR_UNDER_AGE_PROMISE_TRUE As Int
        Sets the tag for users under the age of consent, to comply with the General Data Protection Regulation (GDPR). Ad requests with this tag enabled will be unable to obtain personalized ads.
        UNDER_AGE_PROMISE_TRUE: You want the ad content to meet the ad standard for users under the age of consent.
      • TAG_FOR_UNDER_AGE_PROMISE_UNSPECIFIED As Int
        Sets the tag for users under the age of consent, to comply with the General Data Protection Regulation (GDPR). Ad requests with this tag enabled will be unable to obtain personalized ads.
        UNDER_AGE_PROMISE_UNSPECIFIED: You have not specified whether the ad content should meet the ad standard for users under the age of consent
    • Functions:
      • Initialize
        Initialize RequestOptions
  • HuaweiAdsKitReward
    • Events:
      • RewardAdClosed
      • RewardAdFailedToLoad (Error As String)
      • RewardAdFailedToShow (Error As String)
      • RewardAdOpened
      • Rewarded (RewardName As String, RewardAmount As Int)
      • RewardedLoaded
    • Functions:
      • Initialize (EventName As String, AdId As String)
        Initialize reward ad
      • IsInitialized As Boolean
      • IsLoaded As Boolean
        Check is reward ad loaded
      • LoadAd (Personalized As Boolean, AdContentClassification As String, TagForUnderAgeOfPromise As Int, TagForChildProtection As Int)
        Load reward ad
        Personalized - if is True personlized ad will be loaded, False for non personlized
        AdContentClassification - Sets the maximum ad content rating for the ad requests of your app. The ads obtained using this method have a content rating at or below the specified one.
        TagForUnderAgeOfPromise - Sets the tag for users under the age of consent, to comply with the General Data Protection Regulation (GDPR). Ad requests with this tag enabled will be unable to obtain personalized ads.
        TagForChildProtection - Sets the tag for child-directed content, to comply with the Children's Online Privacy Protection Act (COPPA).
        HuaweiAdsKitReward1.LoadAd(True, RequestOptions1.AD_CONTENT_CLASSIFICATION_W, RequestOptions1.TAG_FOR_UNDER_AGE_PROMISE_TRUE, RequestOptions1.TAG_FOR_CHILD_PROTECTION_TRUE)
      • ShowAd
        Show reward ad
  • HuaweiAdsKitSDK
    • Functions:
      • Initialize
        Initialize HuaweiAdsSDK
        It should be initialized only once in Starter Service on Service_Create
        HuaweiAdsSDK1.Initialize
      • SetVideoMuted (Muted As Boolean)
        Sets whether to mute videos.
      • SetVideoVolume (Volume As Float)
        Sets the video volume. You can use any value between 0.0 - 1.0
        0.0 = set volume to 0%
        1.0 = set volume to 100%


This library is for Huawei phones only, as all Huawei phones already have HMS Core app installed. You may want to use this library for publishing your app on Huawei's AppGallery. Devices without HMS Core app will not show any ads.
For testing on other devices, you need to to download and install HMS Core APK on your device. You will find link in Main activity in example app attached to this post.

Guide how to add App and Ad units.

You should check all activities and Starter service in example app. Please read the commented text, you will find these very important notes.
There are some important things you need to do before publishing your app on AppGallery.
Consent form is not production ready, you need to change privacy and consent text. Everything is described in comments in example app.

Consent form is HTML file. You can edit file, also you can make multiple files with different languages, etc.

For HuaweiAdsKitRequestOptions check official documentation.

IMPORTANT:
to get production ads running, you need to use (as advised from Huawei support):
B4X:
HuaweiBanner1.LoadAd(30, True/False, HuaweiRequestOptions1.AD_CONTENT_CLASSIFICATION_A, HuaweiRequestOptions1.TAG_FOR_UNDER_AGE_PROMISE_UNSPECIFIED, HuaweiRequestOptions1.TAG_FOR_CHILD_PROTECTION_UNSPECIFIED)
Set last three parameters to:
B4X:
HuaweiRequestOptions1.AD_CONTENT_CLASSIFICATION_A
HuaweiRequestOptions1.TAG_FOR_UNDER_AGE_PROMISE_UNSPECIFIED
HuaweiRequestOptions1.TAG_FOR_CHILD_PROTECTION_UNSPECIFIED
In my test only these settings return real ads, any other setting do not show ads with error NO FILL. I'm using these settings for Banner and Interstitial ads.
You can try other options.

Download library zip file from this link and extract archive to Additional Libraries folder.


What's new:

v1.30:
  • Update libraries to newer version
v1.22:
  • Two new methods for SDK: HuaweiAdsSDK.SetVideoVolume and HuaweiAdsSDK.SetVideoMuted

v1.21:
  • Pause/Resume for banner

v1.20:
  • Smart banner

v1.10:
  • Reward Ads
  • Update for example app

v1.00:
  • Consent (consent.html) file - two new links will expose Huawei devices specific intents.
  • Support for COPPA and GDPR in LoadAd method.
  • HuaweiAdsKitSDK.Initialize is in Starter Service not in Main activity.
  • HuaweiAdsKitRequestOptions - Constants for COPPA and GDPR, check official documentation.
  • Two new events for InterstitialAd.
  • Manifest update, as described on this link.
  • Example app is also updated.
 

Attachments

  • HuaweiAdsKitExample.zip
    16.6 KB · Views: 302
Last edited:

hl88

Member
Hi, I'm new to B4A. I was approve to monetize on Huawei's phone yesterday,
Where is the section I can obtain my own ad unit ID? Do I need to sign my app in order to obtain "SHA-256 certificate fingerprint" number from Android-maginfest.xml? Sorry for noob question
Thank you

1603159747699.png
 

Pendrush

Well-Known Member
Licensed User
Longtime User
You can get SHA256 with this java command
B4X:
keytool -list -v -keystore <keystore-file>
or from Tools -> Private Sign Key in B4A

Also read this guide:
 

hl88

Member
I copy paste my Ad unit Id to project, real ad is not show
1603185598907.png

1603185659290.png


Where to paste my App ID? I uploaded dummyapp.apk to AppgalleryConnect>Draft. Does its signed key need to matched with SHA-256 fingerprint I asked earlier?
 

Pendrush

Well-Known Member
Licensed User
Longtime User
Please re-read post #1 and documentation linked in post #1.
Also pay attention on tool-tip when you type code. Every method is documented in tool-tip with example code.
 

hl88

Member
Release the app and real ad will show (step 9) with only ad unit-id, no need app-id in manifest like admob?
0000000000011111111.20200814174025.60553209551551173497922847554393:50511009065854:2800:DCDFDEBE02E527D714C2D1B58EC11D261C83F3884F6D188BB784EDB977A5A00F.png
 

Almora

Active Member
Licensed User
Longtime User
The sample application works fine on my HMS installed device. showing ads.
When I integrate the app into my app in app gallery, it doesn't show ads.

error log..

NeedConsent - we will show Consent dialog
Load Personalized Ad
Banner AdFailed: ErrorCode: 3
Interstitial AdFailed: ErrorCode: 3

huawei document
public const int NoAd
The ad request is sent successfully. However, no ad is returned due to a lack of ad resources.

Constant value: 3

huawei advertising service may differ depending on the country
 

Pendrush

Well-Known Member
Licensed User
Longtime User
Did you load banner like this?
B4X:
HuaweiBanner1.LoadAd(30, True/False, HuaweiRequestOptions1.AD_CONTENT_CLASSIFICATION_A, HuaweiRequestOptions1.TAG_FOR_UNDER_AGE_PROMISE_UNSPECIFIED, HuaweiRequestOptions1.TAG_FOR_CHILD_PROTECTION_UNSPECIFIED)
- You app (any version) MUST be published on AppGallery.
- SHA256 must be the same on their site (you need to submit it) as in your keystore file.
 

Almora

Active Member
Licensed User
Longtime User
Did you load banner like this?
B4X:
HuaweiBanner1.LoadAd(30, True/False, HuaweiRequestOptions1.AD_CONTENT_CLASSIFICATION_A, HuaweiRequestOptions1.TAG_FOR_UNDER_AGE_PROMISE_UNSPECIFIED, HuaweiRequestOptions1.TAG_FOR_CHILD_PROTECTION_UNSPECIFIED)
- You app (any version) MUST be published on AppGallery.
- SHA256 must be the same on their site (you need to submit it) as in your keystore file.

True/False
I've tried
The application is online at the app gallery.
I added the SHA256 in the app on the developer page but ....????????(images)
 

Attachments

  • SharedScreens1hot.jpg
    SharedScreens1hot.jpg
    63.8 KB · Views: 259
  • SharedScreenshot.jpg
    SharedScreenshot.jpg
    101.5 KB · Views: 259

Almora

Active Member
Licensed User
Longtime User
edit..
I added a only banner ad and it shows ads...
 
Last edited:

Pendrush

Well-Known Member
Licensed User
Longtime User
Follow instructions in post #1 and everything will work. I'm using this library in production last few months.
 

skaliwag

Member
Licensed User
Longtime User
It looks like something has changed in the latest HMS Core.
With version 5.1.0.309 all ads on a non-Huawei device are now returning ErrorCode : 0
Have Huawei stopped serving ads to non-Huawei devices?
I get the same results with the Huawei Android Studio Java example.
 
Last edited:
Top