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:

andymc

Well-Known Member
Licensed User
Longtime User
I've just updated to use firebase ads but am now getting this error when starting my game:

** Activity (main) Resume **
main_activity_resume (java line: 916)
java.lang.RuntimeException: Object should first be initialized (AdView).
Did you forget to call Activity.LoadLayout?
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.admobwrapper.AdViewWrapper.Resume(AdViewWrapper.java:140)
at uk.co.coffeeinducedgames.invaders.main._activity_resume(main.java:916)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)


I initialise my adview object in the activity_create method as I always have but now this error is showing. I have also moved my target SDK up to 24 from 16 if that makes any difference.
 

DonManfred

Expert
Licensed User
Longtime User
>I've just updated to use firebase

Did you replaced the old adview view from your layout and replaced it with the adview from firebaseads? I mean; if there are any views in the designer (i dont use Ads so i can´t answer this). Just an idea...
 
Last edited:

Vincenzo Fabiano

Member
Licensed User
Longtime User
I have this error on compile....

B4X:
B4A Versione: 7.30
Analisi del Codice.    (0.09s)
Compilazione del codice.    (0.16s)
Compilazione del codice di layouts    (0.02s)
Organizzazione Librerie.    (0.00s)
Generazione file R.    Error
AndroidManifest.xml:33: error: Error: No resource found that matches the given name (at 'label' with value '@string/app_name').
AndroidManifest.xml:41: error: Error: No resource found that matches the given name (at 'value' with value '@integer/google_play_services_version').
AndroidManifest.xml:99: error: Error: No resource found that matches the given name (at 'theme' with value '@style/Theme.IAPTheme').

Any solution? :/
 
Last edited:

Computersmith64

Well-Known Member
Licensed User
Longtime User
I have this error on compile....

B4X:
B4A Versione: 7.30
Analisi del Codice.    (0.09s)
Compilazione del codice.    (0.16s)
Compilazione del codice di layouts    (0.02s)
Organizzazione Librerie.    (0.00s)
Generazione file R.    Error
AndroidManifest.xml:33: error: Error: No resource found that matches the given name (at 'label' with value '@string/app_name').
AndroidManifest.xml:41: error: Error: No resource found that matches the given name (at 'value' with value '@integer/google_play_services_version').
AndroidManifest.xml:99: error: Error: No resource found that matches the given name (at 'theme' with value '@style/Theme.IAPTheme').

Any solution? :/
The manifest can't find those resources. Have you created a values file & placed it in your resources directory? If so, did you make it read only?

If you haven't created that file, I suggest you read the Firebase tutorials - firstly the one about integrating firebase generally, then the one about firebase ads.

- Colin.
 

Vincenzo Fabiano

Member
Licensed User
Longtime User
I fixed whit this and removed my res folder (obsolete):

#AdditionalJar: com.google.android.gms:play-services-analytics
#AdditionalJar: com.google.android.gms:play-services-ads
 

analizer3816

Member
Licensed User
Longtime User
please add admob native advanced
B4X:
https://developers.google.com/admob/android/native-advanced

because google send email to me that admob native express will not work on march 1,2018
and cannot create new native express after october 23,2017
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Native Ads advanced is not publicly available. It is in a limited beta state.
That's true, but the documentation is available online. I have received an email from AdMob asking me to try out Native Advanced ads & links to the relevant documentation. I can try it on my iOS apps (because I wrote them in Swift) but I'm not sure about my Android ones developed with B4A, as I assume the FirebaseAdMob library doesn't support them.

Here's a link to the documentation - I don't know if you can get test ads if you're not a beta tester, but it might be useful...

https://developers.google.com/admob/android/native-advanced

- Colin.
 
Last edited:

hasexxl1988

Active Member
Licensed User
Longtime User
Hello,
I'm doing something wrong and I'm not on it.

The test IDs work perfectly.

Not my own.

I've created the Firebase account (Appname is the same as Admob or B4A.)
I've linked Firebase to Admob.
I have created 2 adblocks (1x banner, 1x interstitial)

But I'm not shown anything, neither banner nor interstitial.

google.services.json from Firebase is in the B4A Projectfolder.

i have Add all Texts to Mainfest from: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/

Ad1.jpg

Ad2.jpg
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hello,
I'm doing something wrong and I'm not on it.

The test IDs work perfectly.

Not my own.

I've created the Firebase account (Appname is the same as Admob or B4A.)
I've linked Firebase to Admob.
I have created 2 adblocks (1x banner, 1x interstitial)

But I'm not shown anything, neither banner nor interstitial.

google.services.json from Firebase is in the B4A Projectfolder.

i have Add all Texts to Mainfest from: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/

Ad1.jpg

Ad2.jpg
Can you post your code?
 

hasexxl1988

Active Member
Licensed User
Longtime User
Can you post your code?
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private BannerAd As AdView
    Private IAd As InterstitialAd
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")

    IAd.Initialize("iad", "MyInterstitialID")
    IAd.LoadAd
End Sub

Sub Activity_Click
End Sub

Sub IAD_AdClosed
    IAd.LoadAd 'prepare a new ad
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button2_Click
    BannerAd.Initialize2("BannerAd", "MyBannerID", 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
End Sub

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

I have 2 buttons added for testing each Ads. Works with the Test IDs.

EDIT:

Do I need to enter the Publisher-ID of Admob, or the Firebase App-ID / Web API-Key somewhere?
 
Last edited:

Computersmith64

Well-Known Member
Licensed User
Longtime User
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private BannerAd As AdView
    Private IAd As InterstitialAd
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")

    IAd.Initialize("iad", "MyInterstitialID")
    IAd.LoadAd
End Sub

Sub Activity_Click
End Sub

Sub IAD_AdClosed
    IAd.LoadAd 'prepare a new ad
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button2_Click
    BannerAd.Initialize2("BannerAd", "MyBannerID", 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
End Sub

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

I have 2 buttons added for testing each Ads. Works with the Test IDs.

EDIT:

Do I need to enter the Publisher-ID of Admob, or the Firebase App-ID / Web API-Key somewhere?

Your ad id ("MyInterstitialID") should look something like "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx". You should also implement the ReceivedAd & FailedToReceiveAd callbacks so that you can get more visibility into whether you are actually receiving the ads or if there's an error somewhere.

- Colin.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
B4X:
Private Sub adMob_ReceiveAd
    Log("ad loaded")
End Sub

Private Sub adMob_FailedToReceiveAd(ErrorCode As String)
    Log("failed to load ad: " & ErrorCode)
End Sub
 

hasexxl1988

Active Member
Licensed User
Longtime User
Your ad id ("MyInterstitialID") should look something like "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx". You should also implement the ReceivedAd & FailedToReceiveAd callbacks so that you can get more visibility into whether you are actually receiving the ads or if there's an error somewhere.

- Colin.
i have MyInterstitialID removed for posting here.

Callback in the Log is Errorcode 0:
Failed: 0
 
Last edited:

Computersmith64

Well-Known Member
Licensed User
Longtime User
i have MyInterstitialID removed for posting here. The ID for Interstitial is: ca-app-pub-4940153087607272/5013832462

Callback in the Log is Errorcode 0:
Failed: 0
From Google:

"It could be that you have only recently created a new Ad Unit ID and requesting for live ads. It could take a few hours for ads to start getting served if that is that case. If you are receiving test ads then your implementation is fine. Just wait a few hours and see if you are able to receive live ads then. If not, can send us your Ad Unit ID for us to look into."

Also, it could just be that there are no ads available...

- Colin.
 
Status
Not open for further replies.
Top