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:

Computersmith64

Well-Known Member
Licensed 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?
Yes - just call the LoadAd method. You have a couple of obvious choices there:

1) In a game, call LoadAd at a certain point in the game / level where you are pretty confident the user isn't going to restart before the end & there is enough time to load the ad before you want to show it (it can take a few seconds if the data rate is slow). This way you won't see a high Requests to Impressions ratio due to ads being loaded but then not shown.

2) If you're not worried about the Requests to Impressions ratio, just call LoadAd as soon as you've initialized your Interstitial object.

You definitely should pre-load interstitials, otherwise the user will see a significant delay when you go to show an ad - not good.

Remember that you have to call LoadAd after you have shown each interstitial, so the easiest way to do that is just call it in the AdClosed method.

- Colin.
 
Last edited:

moster67

Expert
Licensed User
Thanks Colin - your info clarified lots of things for me.
 

Douglas Farias

Expert
Licensed User
hi, @Erel
can you please change the refresh time of bannerads?
if the user dont have internet the event FailedToReceiveAd will be called later 1 min using banner ads.

B4X:
Sub BannerAd_FailedToReceiveAd (ErrorCode As String)
    csu.CallSubDelayedPlus(Me,"Recarrega_Banner",1000)
    Log("try again banner, try again every 1 sec")
End Sub

Sub Recarrega_Banner
   BannerAd.Load
End Sub

with interstitial i dont need to wait this time, if FailedToReceiveAd on interstitial i got the event fired at the same time (max 1 second), no delay.

here is some logs
eglSurfaceAttrib not implemented
Failed to set EGL_SWAP_BEHAVIOR on surface 0xe727e1e0, error=EGL_SUCCESS
Starting ad request.
Use AdRequest.Builder.addTestDevice("AF69E873383B83BFF56480472F03DACC") to get test ads on this device.
There was a problem getting an ad response. ErrorCode: 2
Scheduling ad refresh 60000 milliseconds from now.
Failed to load ad: 2
try again banner, try again every 1 sec
Starting ad request.
Use AdRequest.Builder.addTestDevice("AF69E873383B83BFF56480472F03DACC") to get test ads on this device.
There was a problem getting an ad response. ErrorCode: 2
Scheduling ad refresh 60000 milliseconds from now.
Failed to load ad: 2
try again banner, try again every 1 sec
Starting ad request.
Use AdRequest.Builder.addTestDevice("AF69E873383B83BFF56480472F03DACC") to get test ads on this device.
There was a problem getting an ad response. ErrorCode: 2
Scheduling ad refresh 60000 milliseconds from now.
Failed to load ad: 2
try again banner, try again every 1 sec
Starting ad request.
Use AdRequest.Builder.addTestDevice("AF69E873383B83BFF56480472F03DACC") to get test ads on this device.
There was a problem getting an ad response. ErrorCode: 2
Scheduling ad refresh 60000 milliseconds from now.
Failed to load ad: 2
try again banner, try again every 1 sec
Starting ad request.
Use AdRequest.Builder.addTestDevice("AF69E873383B83BFF56480472F03DACC") to get test ads on this device.
There was a problem getting an ad response. ErrorCode: 2
Scheduling ad refresh 60000 milliseconds from now.
Failed to load ad: 2


many thx
 

Erel

Administrator
Staff member
Licensed User
The refresh rate is set in AdMob console:

SS-2016-11-06_09.53.38.png
 

mkvidyashankar

Active Member
Licensed User
i am getting this error while integrating firebase admob

PackageAdded: package:com.orange.sketch1
** Activity (main) Create, isFirst = true **
Error occurred on line: 153 (Main)
java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@Integer/google_play_services_version" />
at com.google.android.gms.common.zze.zzbp(Unknown Source)
at com.google.android.gms.common.zze.isGooglePlayServicesAvailable(Unknown Source)
at com.google.android.gms.common.zzc.isGooglePlayServicesAvailable(Unknown Source)
at com.google.android.gms.ads.internal.util.client.zza.zzap(Unknown Source)
at com.google.android.gms.ads.internal.client.zzl.zza(Unknown Source)
at com.google.android.gms.ads.internal.client.zzl.zza(Unknown Source)
at com.google.android.gms.ads.internal.client.zzae.zzlh(Unknown Source)
at com.google.android.gms.ads.internal.client.zzae.zzlg(Unknown Source)
at com.google.android.gms.ads.internal.client.zzae.zza(Unknown Source)
at com.google.android.gms.ads.BaseAdView.loadAd(Unknown Source)
at com.google.android.gms.ads.AdView.loadAd(Unknown Source)
at anywheresoftware.b4a.admobwrapper.AdViewWrapper.LoadAd(AdViewWrapper.java:119)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:753)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at com.orange.sketch1.main.afterFirstLayout(main.java:102)
at com.orange.sketch1.main.access$000(main.java:17)
at com.orange.sketch1.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)

manifest is as follows
This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" />
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'************ Firebase Base ************
CreateResourceFromFile("google-services", "google-services.json")
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
AddPermission(${applicationId}.permission.C2D_MESSAGE)
AddManifestText( <permission android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />)
AddApplicationText(
<receiver
android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.measurement.UPLOAD"/>
</intent-filter>
</receiver>

<service
android:name="com.google.android.gms.measurement.AppMeasurementService"
android:enabled="true"
android:exported="false"/>
<provider
android:authorities="${applicationId}.firebaseinitprovider"
android:name="com.google.firebase.provider.FirebaseInitProvider"
android:exported="false"
android:initOrder="100" />
<receiver
android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.measurement.UPLOAD"/>
</intent-filter>
</receiver>

<service
android:name="com.google.android.gms.measurement.AppMeasurementService"
android:enabled="true"
android:exported="false"/>
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
android:exported="false" />


<service
android:name="com.google.firebase.iid.FirebaseInstanceIdService"
android:exported="true">
<intent-filter android:priority="-500">
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
)
'************ Firebase Base (end) ************

'************ Firebase Ads ************
AddApplicationText(
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
<activity android:name="com.google.android.gms.ads.purchase.InAppPurchaseActivity"
android:theme="@style/Theme.IAPTheme"/>
)
'************ Firebase Ads (end) ************
'End of default text.

how to correct this?
Thanks in advance :)
 
Last edited:

mkvidyashankar

Active Member
Licensed User
Thank you very much
it is working

In other program i followed the same procedure but getting this error
java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@Integer/google_play_services_version" />

Manifest is
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="9" />
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddApplicationText(<meta-data android:name="com.google.android.gms.version" android:value="@Integer/google_play_services_version" />)
'End of default text.
'************ Google Play Services Base ************
AddApplicationText(
<activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="false"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@Integer/google_play_services_version" />
)
'************ Google Play Services Base (end) ************
'************ Firebase Base ************
CreateResourceFromFile("google-services", "google-services.json")
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
AddPermission(${applicationId}.permission.C2D_MESSAGE)
AddManifestText( <permission android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />)
AddApplicationText(
<receiver
android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.measurement.UPLOAD"/>
</intent-filter>
</receiver>

<service
android:name="com.google.android.gms.measurement.AppMeasurementService"
android:enabled="true"
android:exported="false"/>
<provider
android:authorities="${applicationId}.firebaseinitprovider"
android:name="com.google.firebase.provider.FirebaseInitProvider"
android:exported="false"
android:initOrder="100" />
<receiver
android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.measurement.UPLOAD"/>
</intent-filter>
</receiver>

<service
android:name="com.google.android.gms.measurement.AppMeasurementService"
android:enabled="true"
android:exported="false"/>
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
android:exported="false" />


<service
android:name="com.google.firebase.iid.FirebaseInstanceIdService"
android:exported="true">
<intent-filter android:priority="-500">
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
)
'************ Firebase Base (end) ************
'************ Firebase Ads ************
AddApplicationText(
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
<activity android:name="com.google.android.gms.ads.purchase.InAppPurchaseActivity"
android:theme="@style/Theme.IAPTheme"/>
)
'************ Firebase Ads (end) ************


where i am doing error? pl help
 

catyinwong

Active Member
Licensed User
The following error occurs:

B4A version: 6.50
Parsing code. (0.42s)
Compiling code. (1.24s)
Compiling layouts code. (0.11s)
Organizing libraries. Error
Maven artifact not found: com.google.firebase/firebase-ads

May I ask way?
 

catyinwong

Active Member
Licensed User
Last edited:

catyinwong

Active Member
Licensed User
Are you connected to the device in USB debug mode? If so then check the unfiltered logs. There might be more information there.

Another thing to try is to first uninstall the existing app.

Here is what listed in the unfiltered log:

Installing file.
** Activity (main) Pause, UserClosed = false **
updateVisibility : ActivityRecord{86bd645 token=android.os.BinderProxy@d622d13 {anywheresoftware.b4a.b4abridge/anywheresoftware.b4a.b4abridge.main}} show : false
** Activity (main) Resume **
Setting install_non_market_apps has moved from android.provider.Settings.Global to android.provider.Settings.Secure, returning read-only value.
Timeline: Activity_idle id: android.os.BinderProxy@d622d13 time:1110728640

And I have already tried uninstalling the previous one before I run the debug again.
 

catyinwong

Active Member
Licensed User
Are you connected to the device in USB debug mode? If so then check the unfiltered logs. There might be more information there.

Another thing to try is to first uninstall the existing app.

Here is what listed in the unfiltered log:

Installing file.
** Activity (main) Pause, UserClosed = false **
updateVisibility : ActivityRecord{86bd645 token=android.os.BinderProxy@d622d13 {anywheresoftware.b4a.b4abridge/anywheresoftware.b4a.b4abridge.main}} show : false
** Activity (main) Resume **
Setting install_non_market_apps has moved from android.provider.Settings.Global to android.provider.Settings.Secure, returning read-only value.
Timeline: Activity_idle id: android.os.BinderProxy@d622d13 time:1110728640

And I have already tried uninstalling the previous one before I run the debug again.
 
Status
Not open for further replies.
Top