B4A Library FirebaseAdMob - Admob ads integrated with Firebase backend

Status
Not open for further replies.
Use FirebaseAdMob2 instead: https://www.b4x.com/android/forum/threads/firebaseadmob2-google-mobile-ads-sdk-v20.129609/

This library required B4A v6+.

This library allows showing AdMob ads. It is integrated with Firebase services so you can better track and analyze the results.
https://firebase.google.com/docs/admob/

It supports banner ads and interstitial ads (full screen ads).

First you need to follow the Firebase integration tutorial: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/
Make sure to include the Ads manifest snippet.

Full example that shows a banner ad and an interstitial ad when clicking on the activity:
B4X:
Sub Process_Globals

End Sub

Sub Globals
   Private BannerAd As AdView
   Private IAd As InterstitialAd
End Sub

Sub Activity_Create(FirstTime As Boolean)
   BannerAd.Initialize2("BannerAd", "ca-app-pub-3940256099942544/6300978111", BannerAd.SIZE_SMART_BANNER)
   Dim height As Int
   If GetDeviceLayoutValues.ApproximateScreenSize < 6 Then
    'phones
    If 100%x > 100%y Then height = 32dip Else height = 50dip
   Else
    'tablets
    height = 90dip
   End If
   Activity.AddView(BannerAd, 0dip, 100%y - height, 100%x, height)
   BannerAd.LoadAd
   IAd.Initialize("iad", "ca-app-pub-3940256099942544/1033173712")
   IAd.LoadAd
End Sub

Sub Activity_Click
   If IAd.Ready Then IAd.Show
End Sub

Sub IAD_AdClosed
   IAd.LoadAd 'prepare a new ad
End Sub
These are test ids and they will show test ads.

Edit: It is also required to request user consent for personalized ads. See this tutorial: https://www.b4x.com/android/forum/threads/firebaseadmob-and-user-consent.93347/#post-590593

V1.60 - Few new requirements, explained here: https://www.b4x.com/android/forum/threads/firebaseadmob-v1-6.108552/
V1.50 - Adds support for requesting consent.
V1.31 - Fixes an issue related to InMobi mediation ads.
V1.30 - Adds support for rewarded video ads: https://www.b4x.com/android/forum/threads/firebaseadmob-rewarded-video-ads.71430/
V1.20 - Adds support for native ads: https://www.b4x.com/android/forum/t...integrated-with-firebase-backend.67710/page-3
https://www.b4x.com/android/forum/t...integrated-with-firebase-backend.67710/page-3
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
V1.20 is now available for download. It adds support for native ads.

upload_2016-8-30_10-23-5.png



Native ads are similar to banner ads (AdView). You can customize their looks and their size is dynamic.

The code for native ads is almost identical to banner ads:
B4X:
Sub Globals
   Private adview1 As NativeExpressAd
End Sub

Sub Activity_Create(FirstTime As Boolean)
   adview1.Initialize("Ad", "ca-app-pub-126757084444444/44444444", 100%x, 200dip) 'change the ad unit id
   Activity.AddView(adview1, 0, 0,  100%x, 200dip)
   adview1.LoadAd
End Sub


Sub Ad_FailedToReceiveAd (ErrorCode As String)
  Log("failed: " & ErrorCode)
End Sub
Sub Ad_ReceiveAd
  Log("received")
End Sub
Sub Ad_AdScreenDismissed
   Log("Dismissed")
End Sub
Sub Activity_Pause (UserClosed As Boolean)
   adview1.Pause
End Sub

Sub Activity_Resume
   adview1.Resume
End Sub

The differences are:

1. The ad unit id should be of a native ad.
2. There is no predefined size.

The customization is done in AdMob developer console.

SS-2016-08-30_10.25.37.png
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi there. Please, as usual, excuse me for my "noobing" :)

This is the first time I'm tring to use AdMod and, obviously, the first time I'm tring to configure it in B4A.

I followed the tutorials, all went ok: now in my app (still not released!) I can see the test-ads-banner.

But ... how to switch to the "real one"?

I mean: I registered in Firebase as suggested, but there's a key or something I've to put in my app?

I mean something to substitute this line of code:

B4X:
BannerAd.Initialize2("BannerAd", "ca-app-pub-3940256099942544/6300978111", BannerAd.SIZE_SMART_BANNER)

anyway I can't find it in my Firebase control panel ...

Thank you!
You need to replace the "ca-app..." with the id of the ad you created in the AdMob console.

- Colin.
 

capisx

Member
Licensed User
Longtime User
I hope the admob rewarded interstitial will be soon added too, it will be great for games monetization.
 

itgirl

Active Member
Licensed User
Longtime User
Thank you for supporting the native ads but i've noticed that the ads only work with the styles [M001,M002,M003] but when i try the bigger style lets say L003 it does not work and return failed with 0 ? any ideas ?
 

itgirl

Active Member
Licensed User
Longtime User
Have you checked the unfiltered logs for any messages?
it works now Infact turn out that it's just needs time to activate the adunit. usually it takes 1 hour but the new native ads took 1 day to show ads. i wasnt sure whats wonrg , anyways thank you ❤ Erel ❤
 

susu

Well-Known Member
Licensed User
Longtime User
I added Firebase Admob to app 1 and it worked correctly.

I just added Firebase Admob to app 2 with the same steps (same Manifes, same code, changed new AdMob Ad Unit ID, new package name too...) but it doesn't work!

Unfilter log here:
B4X:
Received error HTTP response code: 403
There was a problem getting an ad response. ErrorCode: 0
Failed to load ad: 0
Starting ad request.
Use AdRequest.Builder.addTestDevice("XXXXXXXXXXXXXXXXXXXXX") to get test ads on this device.

I can't understand what's wrong here? :eek: (And I created new Admob ID for 12 hours ago).
 

Pendrush

Well-Known Member
Licensed User
Longtime User
Here is @Erel code for initializing smart banner:
B4X:
BannerAd.Initialize2("BannerAd", "ca-app-pub-3940256099942544/6300978111", BannerAd.SIZE_SMART_BANNER)
Dim height As Int
If GetDeviceLayoutValues.ApproximateScreenSize < 6 Then
    'phones
    If 100%x > 100%y Then height = 32dip Else height = 50dip
Else
    'tablets
    height = 90dip
End If
Activity.AddView(BannerAd, 0dip, 100%y - height, 100%x, height)
BannerAd.LoadAd

Actually this code refuse to show smart banner on some devices (ErrorCode: 3), one of them is Samsung Note 5 (link). Samsung Note 5 return ApproximateScreenSize about 5.2" and we force AdMob to show Ad height 50dip (in portrait), but AdMob server actually return Ad with 90dip height, so no room for Ad to show and as result Ad space is blank in application with ErrorCode 3.
I have found solution today.
In Erel's code we check device ApproximateScreenSize, but this is wrong. We actually need to check device screen height (scaled), as you can found here.

Solution code work in both mode, portrait and landscape:
B4X:
BannerAd.Initialize2("BannerAd", "ca-app-pub-3940256099942544/6300978111", BannerAd.SIZE_SMART_BANNER)   
Dim AdMobHeight As Int
Dim ScreenHeightScaled As Float
ScreenHeightScaled = GetDeviceLayoutValues.Height / GetDeviceLayoutValues.Scale
' Ad height 32dip: device screen height <= 400
' Ad height 50dip: 400 < device screen height <= 720
' Ad height 90dip: device screen height > 720
If ScreenHeightScaled <= 400 Then
    AdMobHeight = 32dip
Else if ScreenHeightScaled > 400 And ScreenHeightScaled <= 720 Then
    AdMobHeight = 50dip
Else if ScreenHeightScaled > 720 Then
    AdMobHeight = 90dip
End If
Activity.AddView(BannerAd, 0dip, 100%y - AdMobHeight, 100%x, AdMobHeight)


EDIT 2:
Removed non-working code.
We need a way to determine Screen size and Scale in designer, but I really don't know proper way.
 
Last edited:

victormedranop

Well-Known Member
Licensed User
Longtime User
Hello, one question i create a banner in admob.
in the confirmation email i receive two ca-app-pub

wich one is AdView and the InterstitialAd.

Thanks Victor
 

victormedranop

Well-Known Member
Licensed User
Longtime User
I am using this function to see waths happend
B4X:
Sub iad_FailedToReceiveAd (ErrorCode As String)
    Log("not Received - " &"Error Code: "&ErrorCode)
    IAd.LoadAd
End Sub

and found tat error 3 is that admob does not have any add for my region.
it's true?
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hello, one question i create a banner in admob.
in the confirmation email i receive two ca-app-pub

wich one is AdView and the InterstitialAd.

Thanks Victor

If you go into your AdMob console & click on the app name, it will show you the ads that have been set up for that app, what type they are & the id of the ad (eg: "ca-app-pub...").

- Colin.
 

moster67

Expert
Licensed User
Longtime User
Hi, reading the firebase docs, it says it could be a good idea to pre-load the interstitial ads to avoid latency. Is this possible with the B4A implementation? If not, what is your experience using interstitial ads? Do they tend to open slowly?
 
Status
Not open for further replies.
Top