Android Tutorial FirebaseAdMob - Rewarded video ads

Edit: Google AdMob does support rewarded ads without mediation.
You can use this unit id to test it: rewardAd.LoadAd("ca-app-pub-3940256099942544/5224354917")
Make sure to use FirebaseAdMob v1.51+

FirebaseAdMob v1.30 adds support for rewarded video ads. Rewarded video ads are video ads where the user is rewarded if he watches the full video.
How is the user rewarded is up to you. In many games for example, the users will receive an extra life if they watch the video ad.

Google AdMob do not provide these ads. You need to use the mediation feature to add an ad network that does support rewarded videos. You can see the features that each ad network supports here: https://firebase.google.com/docs/admob/android/mediation-networks#supported-ad-formats
I've tested it with InMobi.

The first step is to integrate the third party ad network as explained here: AdMob Mediation (with InMobi)

Working with RewardedVideoAd is similar to working with InterstitialAds. You first need to call LoadAd. Note that the ad unit is passed to the LoadAd method and not to the Initialize method.

The ReceiveAd event will be raised when an ad is ready. Now you can call Ad.Show to show the ad. If the user will fully watch the ad then the Rewarded event will be raised.

Example:
B4X:
Sub Globals
   Private ad As RewardedVideoAd
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ad.Initialize("ad")
End Sub

Sub Activity_Resume
   ad.LoadAd("ca-app-pub-12345675929340/33333326118")
End Sub

Sub ad_ReceiveAd
   Log("Ad received. Now wait for the right moment to show the ad.")
End Sub

Sub Ad_Rewarded (Item As Object)
   'Item is currently not used
   ToastMessageShow("You are rewarded!!!", True)
End Sub

Sub Ad_FailedToReceiveAd (ErrorCode As String)
  Log("Failed: " & ErrorCode)
End Sub

Sub Ad_AdClosed
   Log("Closed")
End Sub

Sub Ad_AdOpened
   Log("Opened")
End Sub

Sub Activity_Click
   If ad.Ready Then ad.Show
End Sub
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to load an ad with a test device (instead of calling LoadAd);
B4X:
Dim builder As JavaObject
builder.InitializeNewInstance("com.google.android.gms.ads.AdRequest.Builder", Null)
builder.RunMethod("addTestDevice", Array(Test device string here))
Dim jo As JavaObject = RewardedVideoAd1
jo.RunMethod("loadAd", Array(builder.RunMethod("build", Null))
 

alimanam3386

Active Member
Licensed User
Longtime User
You can use this code to load an ad with a test device (instead of calling LoadAd);
B4X:
Dim builder As JavaObject
builder.InitializeNewInstance("com.google.android.gms.ads.AdRequest.Builder", Null)
builder.RunMethod("addTestDevice", Array(Test device string here))
Dim jo As JavaObject = RewardedVideoAd1
jo.RunMethod("loadAd", Array(builder.RunMethod("build", Null))

Thank you Erel, Can I pass any string variable for test device string right ?
 

Feten7

Member
Licensed User
Hi,

What i don't understand is why in some devices it keeps restarting the activity and sometimes the whole app. I don't want to put the app on Play Store until i'm sure it's going to work. Here are the logs

LogCat connected to: 127.0.0.1:21503
--------- beginning of /dev/log/system--------- beginning of /dev/log/main~i:*** Service (starter) Create ***~i:** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
received
received
** Activity (main) Pause, UserClosed = false **
** Activity (nopubli) Create, isFirst = true **
** Activity (nopubli) Resume **
** Activity (nopubli) Pause, UserClosed = false **
sending message to waiting queue (ad_adopened)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (nopubli) Create, isFirst = true **
** Activity (nopubli) Resume **
** Activity (nopubli) Pause, UserClosed = false **
sending message to waiting queue (ad_adopened)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (nopubli) Create, isFirst = true **
** Activity (nopubli) Resume **
** Activity (nopubli) Pause, UserClosed = false **
sending message to waiting queue (ad_adopened)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (nopubli) Create, isFirst = true **
** Activity (nopubli) Resume **
** Activity (nopubli) Pause, UserClosed = true **
And here the code
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private ad As RewardedVideoAd
    Dim dato As RandomAccessFile
    Dim fecha As Long
    Dim intentos As Int
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")
    ad.Initialize("ad")
    intentos=0


End Sub

Sub Activity_Resume
    ad.LoadAd("ca-app-pub-9543716757242777/7172194814")
    Msgbox2Async("Ici, après d'avoir vu un petit vidéo, vous aurez l'app libre de publicité pour un mois. Voulez vous continuer?","Se débarraser de la publicité","Oui","","Non",Null,False)
    Wait For MsgBox_Result (result As Int)
    If result = DialogResponse.NEGATIVE Then
        Activity.Finish
    End If
End Sub
Sub ad_ReceiveAd
    ad.Show
End Sub
Sub Ad_Rewarded (Item As Object)
    fecha=DateTime.Now
    File.OpenOutput(File.DirDefaultExternal,"data.txt",False)
    dato.Initialize(File.DirDefaultExternal,"data.txt",False)

    dato.WriteLong(fecha,0)
    dato.Close
    Msgbox("Vous avez gagné un mois sans publicité! Merci","Fini")
    StartActivity(Main)
End Sub

Sub Ad_FailedToReceiveAd (ErrorCode As String)
    If intentos<30 Then
    Log("Failed: " & ErrorCode)
    ad.LoadAd("ca-app-pub-9543716757242777/7172194814")
    intentos=intentos+1
    Else
    Msgbox("Il y a quelques problèmes","Problèmes")
    Activity.Finish
    End If
End Sub

Sub Ad_AdClosed

End Sub

Sub Ad_AdOpened

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Can someone help me?

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Thank you Erel, Can I pass any string variable for test device string right ?
The test device string should be printed in the logs.

Can someone help me?
You aren't calling ExitApplicaton somewhere, right?

Try to call Service.StartForeground in the starter service before you show the ad.
 

Feten7

Member
Licensed User
Hi,

You aren't calling ExitApplicaton somewhere, right?

No, i'm not calling ExitApplication.

Try to call Service.StartForeground in the starter service before you show the ad.

Sorry but i don't understand, must i create a service in order to show the ad?
 

jchal

Active Member
Licensed User
Longtime User
i tryed to run the example but i get error code 0 what does that means , i dont see any adds at all
 

alimanam3386

Active Member
Licensed User
Longtime User
Hi

When I use the Erel code in post1 of this topic I get this error:

B4X:
sending message to waiting queue (ad_adopened)
java.lang.AbstractMethodError: abstract method "void com.google.android.gms.ads.reward.RewardedVideoAdListener.onRewardedVideoCompleted()"
    at com.google.android.gms.internal.ads.zzahj.onRewardedVideoCompleted(Unknown Source)
    at com.google.android.gms.internal.ads.zzahf.dispatchTransaction(Unknown Source)
    at com.google.android.gms.internal.ads.zzek.onTransact(Unknown Source)
    at android.os.Binder.transact(Binder.java:387)
    at hl.b(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):20)
    at com.google.android.gms.ads.internal.reward.client.l.f(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):27)
    at com.google.android.gms.ads.internal.a.u(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):471)
    at com.google.android.gms.ads.internal.reward.d.f_(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):87)
    at com.google.android.gms.ads.internal.reward.mediation.h.h(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):37)
    at com.google.android.gms.ads.internal.reward.client.n.f(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):96)
    at com.google.android.gms.ads.internal.a.u(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):471)
    at com.google.android.gms.ads.internal.ak.Z(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):277)
    at com.google.android.gms.ads.internal.gmsg.am.a(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):28)
    at com.google.android.gms.ads.internal.webview.j.b(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):356)
    at com.google.android.gms.ads.internal.webview.ab.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at com.google.android.gms.ads.internal.util.f.dispatchMessage(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):9)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7225)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

TargetSdk in manifest set to 26 , and I tested this code on Samsung note4 , What's wrong ?
 
Top