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,085
Last edited:

tufanv

Expert
Licensed User
Longtime User
Anyone who want to use User Messaging Platform, need to check this link, as user can turn off your ads completely.
As I know there is no working solution for now.
Maybe I'm wrong (did not tested Erel's solution), but if you click on Manage Options and on next screen just click on Submit, you will not be able to show even test banner.
You will get error like:
If that is correct, it would be disaster so we need to be sure about it. @Erel you have any idea about it?
 

Pendrush

Well-Known Member
Licensed User
Longtime User
If that is correct, it would be disaster so we need to be sure about it. @Erel you have any idea about it?
You should test it. I have tested in Kotlin few days ago and successfully turned off all AdMob ads.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

yiankos1

Well-Known Member
Licensed User
Longtime User
Old way still working fine with AdMob SDK v20+
I am still using this way and everything works fine! I have not updated to v20 btw
Anyone who want to use User Messaging Platform, need to check this link, as user can turn off your ads completely.
As I know there is no working solution for now.
Maybe I'm wrong (did not tested Erel's solution), but if you click on Manage Options and on next screen just click on Submit, you will not be able to show even test banner.
You will get error like:
This bug persist from day one of UMP and still no fix...
 

tufanv

Expert
Licensed User
Longtime User
I am still using this way and everything works fine! I have not updated to v20 btw

This bug persist from day one of UMP and still no fix...
How do you use it with the new firebaseadmob lib?

If consent.IsRequestLocationInEeaOrUnknown Then

This line gives an error on the new lib.
 

yiankos1

Well-Known Member
Licensed User
Longtime User
How do you use it with the new firebaseadmob lib?

If consent.IsRequestLocationInEeaOrUnknown Then

This line gives an error on the new lib.
No, no... As i mentioned, i did not updated SDK nor AdMob SDK v20+. Did you updated both and it's not working?
 

tufanv

Expert
Licensed User
Longtime User
Updated both and that line is not working and for now I dont want to use the new method in adshelper because of the obvious reason.
 

Pendrush

Well-Known Member
Licensed User
Longtime User
There is two possible scenarios:
1. Old consent can be separate library.
2. Old consent can be part of this new library, like in old AdMob library.

Probably Erel can do it. It's few minutes job.
 

Rubsanpe

Active Member
Licensed User
Thank you for the code. It works really well. My only question is, can you know if the user has clicked the "Consent" or "Nor consent" option and perform any action accordingly?

Rubén
 

tufanv

Expert
Licensed User
Longtime 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.

Do we need to use builder to load ads or use simple loadad as in the tutorial to show ads according to consent?
 
Status
Not open for further replies.
Top