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:

sorex

Expert
Licensed User
Longtime User
@Erel,

in the FireBaseAdmob.xml is this reference

B4X:
<event>AdLeftApplication</event>

is this a

B4X:
sub AdI_AdLeftApplication

even that can be used to know when the app is being closed due to a clicked interstitial so that we can save settings?
 

sorex

Expert
Licensed User
Longtime User
it is... altho the event happends after the ad gets closed since it gets queued. so not really usefull to save app data there.

Edit: silly me, I can save before using the .show command
 

mesutaslan

Member
Licensed User
Longtime User
erel what the avaible events?

IAD_AdClosed etc...

Have event about error? clicked, opened etc...

the iad here is never ready i need to know why :(


Edit. i found this
B4X:
Sub iad_AdClosed
    iad.LoadAd 'prepare a new ad
End Sub

Sub iad_ReceiveAd
    Log("Received")
End Sub

Sub iad_FailedToReceiveAd (ErrorCode As String)
    Log("not Received - " &"Error Code: "&ErrorCode)
    iad.LoadAd
End Sub

Sub iad_adopened
End Sub

----------------------------------------------------------------------

The problem now is the error code: 0

- Make new app intersticial and get the code OK
- Sync with firebase and put the package name to download the json file OK
- Put the json file on the app folder OK

- Download the last sdk, google play services and etc... OK
- Add the permisions code at manifest OK
https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/
i made all this tutorial.

- Start the iad OK
B4X:
   iad.Initialize("iad","ca-app-pub-xxxxxxxxxxxxxx/xxxxxxxxx")
   iad.LoadAd

Error Log : Code 0 FAIL

what can is this error? its new app made only to test, this is not on google play or banned.
the code and tutorials are ok


EDIT
I m reinstaled the google play services again and now its working

thx again


What do you mean reinstalling google play services ?

I am using Google Play Services 31
also
google repistory 29 and android support repistory 33 is installed.

i am getting "not Received - Error Code: 0"

any idea ?
 

Douglas Farias

Expert
Licensed User
Longtime User
delete google play services on android sdk, check and press unistall.
later install again. it works for me
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
Any way we could get this updated to support NativeExpressAds?

Here is a good start for the java code. I can't get it to compile because Eclipse can't find the class due to the new library referencing system.

B4X:
    @BA.ShortName(value="NativeExpressAd")
    public static class NativeExpressAdWrapper
    extends ViewWrapper<NativeExpressAdView> {
    public void Initialize(final BA ba, String EventName, String AdUnitId, Integer Width, Integer Height) {
        NativeExpressAdView ad = new NativeExpressAdView((Context)ba.activity);
        AdSize adSize=new AdSize(Width,Height);
        ad.setAdSize(adSize);
        ad.setAdUnitId(AdUnitId);
        this.setObject((Object)ad);
        super.Initialize(ba, EventName);
        final String eventName = EventName.toLowerCase(BA.cul);
       
       
        ((NativeExpressAdView)this.getObject()).setAdListener(new AdListener(){

            public void onAdFailedToLoad(int e) {
                ba.raiseEvent(NativeExpressAdViewWrapper.this.getObject(), String.valueOf(eventName) + "_failedtoreceivead", new Object[]{String.valueOf(e)});
            }

            public void onAdLoaded() {
                ba.raiseEvent(NativeExpressAdViewWrapper.this.getObject(), String.valueOf(eventName) + "_receivead", new Object[0]);
            }

            public void onAdClosed() {
                ba.raiseEventFromDifferentThread(NativeExpressAdViewWrapper.this.getObject(), (Object)null, 0, String.valueOf(eventName) + "_adscreendismissed", false, null);
            }

            public void onAdLeftApplication() {
            }

            public void onAdOpened() {
                ba.raiseEventFromDifferentThread(NativeExpressAdViewWrapper.this.getObject(), (Object)null, 0, String.valueOf(eventName) + "_presentscreen", false, null);
            }
        });
    }
    public void LoadAd() {
        AdRequest req = new AdRequest.Builder().build();
        ((NativeExpressAdView)this.getObject()).loadAd(req);
    }

    public void Pause() {
        ((NativeExpressAdView)this.getObject()).pause();
    }

    public void Resume() {
        ((NativeExpressAdView)this.getObject()).resume();
    }   
    }
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

abhishek007p

Active Member
Licensed User
Longtime User
Do all AdMob users need to move to firebase admob? or only those who wish to use analytical in their ads.

can anyone tell me, thanks.
 

stefanoa

Active Member
Licensed User
Longtime User
Hi,
I'm trying to replace admob, integrating Firebase as described in the tutorial, but when i compile I get this error:

B4X:
B4A version: 6.00
Parsing code.    (0.73s)
Compiling code.    (1.58s)
Compiling layouts code.    (0.07s)
Organizing libraries.    (0.00s)
Generating R file.    (0.61s)
Compiling debugger engine code.    (7.40s)
Compiling generated Java code.    Error
javac 1.8.0_102
src\xxxxxxx\xxxxxxxxx\events.java:411: error: cannot find symbol
public anywheresoftware.b4a.admobwrapper.AdViewWrapper.InterstitialAdWrapper _iad = null;
                                                      ^
  symbol:   class InterstitialAdWrapper
  location: class AdViewWrapper
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

Can you help me? thanks
 

DonManfred

Expert
Licensed User
Longtime User
public anywheresoftware.b4a.admobwrapper.AdViewWrapper.InterstitialAdWrapper _iad = null;
Looks like you are still using the old admob library and not the firebaseadmob
 

zhonghua

Member
Licensed User
I have 2 questions:

1. Must I publish the app on Google Play to get Ads? I can get Ads by using the test ID ,but when I use my id and my json file, it doesn't show the banner.

2. Can I add some code to set a test device?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Are you handling the FailedToReceiveAd event? It will give more information about the reason for the lack of ads.

You can use this sub to load ads and set the test device id:
B4X:
Sub LoadAdWithTestDevice(Ad As AdView, TestId As String)
   Dim jo As JavaObject
   jo.InitializeNewInstance("com.google.android.gms.ads.AdRequest$Builder", Null)
   jo = jo.RunMethodJO("addTestDevice", Array(TestId)).RunMethod("build", Null)
   Dim jo2 As JavaObject = Ad
   jo2.RunMethod("loadAd", Array(jo))
End Sub

You can see the device id in the unfiltered logs.
 

zhonghua

Member
Licensed User
Actually the log shows "Received",but the banner doesn't show, the IAD ( interstitial ad ) does show when I click ,but only a black screen without any content and with a big 'X' button on the left top.

However, when I use this 'LoadAdWithTestDevice' sub that you provided , it does show a test banner ad.
Maybe my app is too simple (a edit ,some buttons ), Admob does not want to show ads on my app.
I'll try to publish a more complicated app on google play to make sure whether my code is correct or not.
 

StefanoAccorsi

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!
 
Status
Not open for further replies.
Top