B4A Library FirebaseAdMob - Admob ads integrated with Firebase backend

Status
Not open for further replies.

Erel

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




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.

 

Computersmith64

Well-Known Member
Licensed User
Longtime User
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? (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.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…