B4A Library AdsHelper extends FirebaseAdMob2 / Google Mobile Ads v20.0+

Status
Not open for further replies.
AdsHelper is a class that adds the following features:

- Managing the user consent with Google's User Messaging Platform: https://developers.google.com/admob/ump/android/quick-start
- App Open Ads: https://developers.google.com/admob/android/app-open-ads

It can be extended with more features.

Instructions:
1. It is important to read Google's documentation and understand the main concepts.
2 Add to manifest editor:
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
CreateResourceFromFile (Macro, FirebaseAdMob.FirebaseAds)
AddReplacement($ADMOB_APP_ID$, ca-app-pub-1267570xxxxx0~676xxxxx) 'your app id here

3. This class should be used in B4XPages projects. With some work you can use it in non-B4XPages but you will need to handle the much more complicated life cycle and split the tasks between the activity and the starter service.
4. Add to Main:
B4X:
#AdditionalJar: com.google.android.ump:user-messaging-platform


Consent

Sign up to Funding Choices and create an EU consent message: https://support.google.com/fundingchoices/answer/9180084
The logic itself is simple:
B4X:
If Ads.GetConsentStatus = "UNKNOWN" Or Ads.GetConsentStatus = "REQUIRED" Then
    Wait For (Ads.RequestConsentInformation(False)) Complete (Success As Boolean)
End If
If Ads.GetConsentStatus = "REQUIRED" And Ads.GetConsentFormAvailable Then
    Wait For (Ads.ShowConsentForm) Complete (Success As Boolean)
End If
We check the consent status. If it is unknown we request new information.
If the consent status is REQUIRED and a consent form is available then we show it.
The consent status is managed by Google and is cached locally.

In order to debug it we can set a test device and set the location to be in the EU (True) or outside (False):
B4X:
Ads.ResetConsentStatus
Ads.SetConsentDebugParameters("77A04EE40B2AFED2AFC67701365187EC", True)



1618324755085.png


App Open Ads

These are ads that appear when the app has moved to the foreground. It is configured to appear if the app was more than 2 minutes in the background and then resumed.

Instructions:
1. Call Ads.FetchOpenAd once.
2.
B4X:
Private Sub B4XPage_Foreground
    Ads.ShowOpenAdIfAvailable
End Sub

Private Sub B4XPage_Background
    Ads.Background
End Sub

AdsHelper class is included in the attached project.
The project will fail to run unless you set the correct app id in the manifest editor.

Updated version by @Jack Cole, including an example of using AdHelper in non-B4XPages project: https://www.b4x.com/android/forum/threads/adshelper-extension-for-traditional-b4a-apps-more.131798/
 

Attachments

  • AdsHelper.zip
    12.7 KB · Views: 1,108
Last edited:

Rubsanpe

Active Member
Licensed User
Check Ads.GetConsentStatus.

As far as I understand you don't need to do anything with this information, as it is managed by Google.

Thanks for the reply. Pressing any of the two buttons Ads.GetConsentStatus returns OBTAINED. If I have clicked "Consent" the banner is shown without problem, but if I have clicked "Do not consent" then the banner is not shown (logical) and in the FailedToReceiveAd event the error is returned

Failed: {
"Code": 3,
"Message": "No ad config.",
"Domain": "com.google.android.gms.ads",
"Cause": "null",
"Response Info": {
"Response ID": "null",
"Mediation Adapter Class Name": "",
"Adapter Responses": []
}

What I want to do is that if you don't accept the consent you can't continue using the application.

I have been consulting the documentation and it appears that the SDK stores some information in the device preferences based on the IAB requirements https://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework/blob/master/TCFv2/IAB Tech Lab - CMP API v2.md#how-do-third-party-sdks-vendors-access-the-consent-information-in-app.

For example in the key "IABTCF_gdprApplies" is stored if the GDPR is applicable or not on the device, in the key "IABTCF_VendorConsents" is stored the consent that the user has given for each of the advertisers by marking them with 0 or 1, and I have seen that in that value if the user has not given his consent that value is 0.

It is an "indirect" way to know if the consent has been accepted or not. That's why I was asking if there was a "direct" way to know.

Rubén
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can parse the error message. It is a json string.

Or:

B4X:
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim Preferences As JavaObject
Dim SharedPreferences As JavaObject = Preferences.InitializeStatic("android.preference.PreferenceManager").RunMethod("getDefaultSharedPreferences", Array(ctxt))
Dim all As Map = JavaMapToMap(SharedPreferences.RunMethod("getAll", Null))
For Each k As String In all.Keys
    Log(k & ": " & all.Get(k))
Next

Sub JavaMapToMap (javamap As Object) As Map
    Dim m As Map
    m.Initialize
    Dim mo As JavaObject = m
    mo.RunMethod("putAll", Array(javamap))
    Return m
End Sub
 

Rubsanpe

Active Member
Licensed User
I know, thank you. My question was because I understand that this is important information that should be available in the SDK.

Best regards

Ruben
 

Rubsanpe

Active Member
Licensed User
Thank you. I had not contemplated that possibility. The idea is to put advertising in the application. Consent is a must if you want to use advertising in it. I understand that if the user doesn't want to see that advertising he/she doesn't have to be able to use the application.

In the old version of the consent you had the possibility to choose personalized advertising, non-personalized advertising or no advertising. The new version does not give those options. Only accept consent or not accept it.

Rubén
 

tarique

Member
Need help?

In rewarded ads with the following code, how to know that video ads is finished or skipped?
"If Ads.isAvailableRewardedAd Then
Ads.ShowRewardedAd"

I want to use Wait for condition so that I know that either video is skipped or finished?

Thanks
 

quimacama

Member
Licensed User
Longtime User
You can use the old consent code with FirebaseAdMob2.
?It seems that we no longer have this option. I have received several emails...

Action Required: Your app is not compliant with Google Play Policies

Issue: Your app contains content that doesn’t comply with the Families policy.
We have detected that your app includes the deprecated Personalized Ad Consent SDK, which is not approved for use in child-directed services. The SDK has been deprecated in favor of the User Messaging Platform SDK and must be updated.
Apps in the Designed for Families program must only use ad SDKs that have certified their compliance with the Families Ads Program.
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
I wanted to share an update I made to this class and example.

Added:
  • support for rewarded video ads
  • events for rewarded video
  • events for rewarded interstitial
  • events for open ads
  • added parameter for open ads for setting the background delay before allowing the ad to show
  • incorporated native ad example and moved the code into a separate class
  • example of a fixed size banner ad (300x250)
  • support for traditional B4A apps

Modified:
  • code reorganized to make it more readable and easier to follow


1623073008767.png


Examples attached:
B4XPagesMobileAds.zip - You should use this for any new app using B4XPages.
ActivityAdExample.zip - Example of how to use this with a traditional B4A app.
 

Attachments

  • ActivityAdExample.zip
    19.4 KB · Views: 555
  • B4XPagesMobileAds.zip
    18.8 KB · Views: 466
Last edited:

Jack Cole

Well-Known Member
Licensed User
Longtime User
I updated this to support the traditional B4A app type in addition to b4xpages. I had to rewrite a number of things to get the events to work correctly. I also added a parameter to the App Open Ads that allows you to set the background time before the ad can show when it is brought to the foreground. Before this was hardcoded to 2. Native Ads were moved to a separate class mwNative.
 

SMOOTSARA

Active Member
Licensed User
Longtime User
I updated this to support the traditional B4A app type in addition to b4xpages. I had to rewrite a number of things to get the events to work correctly. I also added a parameter to the App Open Ads that allows you to set the background time before the ad can show when it is brought to the foreground. Before this was hardcoded to 2. Native Ads were moved to a separate class mwNative.

Hi Jack Cole
I get the following error in running the examples

I have updated the sdk and there is a Maven file with version 1.0.0
"com.google.android.ump/user-messaging-platform"


B4X:
B4A Version: 10.70
Parsing code.    (0.04s)
    Java Version: 11
Building folders structure.    (0.04s)
Compiling code.    (0.13s)
Compiling layouts code.    (0.00s)
Organizing libraries.    Error
Maven artifact not found: com.google.android.ump/user-messaging-platform
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
Hi Jack Cole
I get the following error in running the examples

I have updated the sdk and there is a Maven file with version 1.0.0
"com.google.android.ump/user-messaging-platform"


B4X:
B4A Version: 10.70
Parsing code.    (0.04s)
    Java Version: 11
Building folders structure.    (0.04s)
Compiling code.    (0.13s)
Compiling layouts code.    (0.00s)
Organizing libraries.    Error
Maven artifact not found: com.google.android.ump/user-messaging-platform

Are you trying to run one of the examples or a different app?

Make sure you have this line in the main module.

B4X:
#AdditionalJar: com.google.android.ump:user-messaging-platform

You can search for user-messaging-platform in the SDK manager to make sure it is installed there. I show version 1.0.0 on my machine.
 

sorex

Expert
Licensed User
Longtime User
Thanks Jack.

Are the events and other things the same as it was before?

And almost 100% equal to what it is in B4i? (changes were minimal with the old lib)
As someone mentioned rewriting/testing these things ain't always fun especially when you didn't have to touch it for years.

I'll try to have a look at it this weekend (football/soccer has some priorities now ;) )
 

sorex

Expert
Licensed User
Longtime User
I don't use open ads but I had rewarded videos and that was the first thing it complained about when I tagged the new and untagged the old lib.

Thanks again.
 

yiankos1

Well-Known Member
Licensed User
Longtime User
Consent

Sign up to Funding Choices and create an EU consent message: https://support.google.com/fundingchoices/answer/9180084
The logic itself is simple:
B4X:
If Ads.GetConsentStatus = "UNKNOWN" Then
Wait For (Ads.RequestConsentInformation(False)) Complete (Success As Boolean)
End If
If Ads.GetConsentStatus = "REQUIRED" And Ads.GetConsentFormAvailable Then
Wait For (Ads.ShowConsentForm) Complete (Success As Boolean)
End If
Hello Erel,

I think the first part of that code should be:
B4X:
    If Ads.GetConsentStatus = "UNKNOWN" Or Ads.GetConsentStatus = "REQUIRED" Then
        Wait For (Ads.RequestConsentInformation(False)) Complete (Success As Boolean)
    End If
Because if i show UMP dialog and user does not choose one of the options and just close app, after this Ads.GetConsentStatus becomes "REQUIRED" and UMP dialog never shows again at next app start.
 
Status
Not open for further replies.
Top