1515#include " firebase/admob.h"
1616#include " firebase/admob/banner_view.h"
1717#include " firebase/admob/interstitial_ad.h"
18+ #include " firebase/admob/rewarded_video.h"
1819#include " firebase/admob/types.h"
1920#include " firebase/app.h"
2021#include " firebase/future.h"
@@ -28,15 +29,15 @@ class LoggingBannerViewListener : public firebase::admob::BannerView::Listener {
2829 LoggingBannerViewListener () {}
2930 void OnPresentationStateChanged (
3031 firebase::admob::BannerView* banner_view,
31- firebase::admob::BannerView::PresentationState new_state ) override {
32- ::LogMessage (" BannerView PresentationState has changed to %d." , new_state );
32+ firebase::admob::BannerView::PresentationState state ) override {
33+ ::LogMessage (" BannerView PresentationState has changed to %d." , state );
3334 }
3435 void OnBoundingBoxChanged (firebase::admob::BannerView* banner_view,
35- firebase::admob::BoundingBox new_box ) override {
36+ firebase::admob::BoundingBox box ) override {
3637 ::LogMessage (
3738 " BannerView BoundingBox has changed to (x: %d, y: %d, width: %d, "
3839 " height %d)." ,
39- new_box .x, new_box .y, new_box .width, new_box .height);
40+ box .x, box .y, box .width, box .height);
4041 }
4142};
4243
@@ -47,19 +48,42 @@ class LoggingInterstitialAdListener
4748 LoggingInterstitialAdListener () {}
4849 void OnPresentationStateChanged (
4950 firebase::admob::InterstitialAd* interstitial_ad,
50- firebase::admob::InterstitialAd::PresentationState new_state) override {
51- ::LogMessage (" InterstitialAd PresentationState has changed to %d." ,
52- new_state);
51+ firebase::admob::InterstitialAd::PresentationState state) override {
52+ ::LogMessage (" InterstitialAd PresentationState has changed to %d." , state);
5353 }
5454};
5555
56+ // A simple listener that logs changes to rewarded video state.
57+ class LoggingRewardedVideoListener
58+ : public firebase::admob::rewarded_video::Listener {
59+ public:
60+ LoggingRewardedVideoListener () {}
61+ void OnRewarded (firebase::admob::rewarded_video::RewardItem reward) override {
62+ ::LogMessage (" Rewarding user with %f %s." , reward.amount,
63+ reward.reward_type.c_str());
64+ }
65+ void OnPresentationStateChanged (
66+ firebase::admob::rewarded_video::PresentationState state) override {
67+ ::LogMessage (" Rewarded video PresentationState has changed to %d." , state);
68+ }
69+ };
70+
71+ // The AdMob app IDs for the test app.
72+ #if defined(__ANDROID__)
73+ const char * kAdMobAppID = " ca-app-pub-3940256099942544~3347511713" ;
74+ #else
75+ const char * kAdMobAppID = " ca-app-pub-3940256099942544~1458002511" ;
76+ #endif
77+
5678// These ad units are configured to always serve test ads.
5779#if defined(__ANDROID__)
5880const char * kBannerAdUnit = " ca-app-pub-3940256099942544/6300978111" ;
5981const char * kInterstitialAdUnit = " ca-app-pub-3940256099942544/1033173712" ;
82+ const char * kRewardedVideoAdUnit = " YOUR_REWARDED_VIDEO_AD_UNIT_ID" ;
6083#else
6184const char * kBannerAdUnit = " ca-app-pub-3940256099942544/2934735716" ;
6285const char * kInterstitialAdUnit = " ca-app-pub-3940256099942544/4411468910" ;
86+ const char * kRewardedVideoAdUnit = " YOUR_REWARDED_VIDEO_AD_UNIT_ID" ;
6387#endif
6488
6589// Standard mobile banner size is 320x50.
@@ -86,7 +110,7 @@ static void WaitForFutureCompletion(firebase::FutureBase future) {
86110 }
87111
88112 if (future.Error () != firebase::admob::kAdMobErrorNone ) {
89- LogMessage (" Action failed with error code %d and message \" %s\" ." ,
113+ LogMessage (" ERROR: Action failed with error code %d and message \" %s\" ." ,
90114 future.Error (), future.ErrorMessage ());
91115 }
92116}
@@ -107,7 +131,7 @@ extern "C" int common_main(int argc, const char* argv[]) {
107131 static_cast <int >(reinterpret_cast <intptr_t >(app)));
108132
109133 LogMessage (" Initializing the AdMob with Firebase API." );
110- firebase::admob::Initialize (*app);
134+ firebase::admob::Initialize (*app, kAdMobAppID );
111135
112136 firebase::admob::AdRequest request;
113137 // If the app is aware of the user's gender, it can be added to the targeting
@@ -156,13 +180,15 @@ extern "C" int common_main(int argc, const char* argv[]) {
156180 ad_size.height = kBannerHeight ;
157181
158182 LogMessage (" Creating the BannerView." );
159- LoggingBannerViewListener banner_listener;
160183 firebase::admob::BannerView* banner = new firebase::admob::BannerView ();
161- banner->SetListener (&banner_listener);
162184 banner->Initialize (GetWindowContext (), kBannerAdUnit , ad_size);
163185
164186 WaitForFutureCompletion (banner->InitializeLastResult ());
165187
188+ // Set the listener.
189+ LoggingBannerViewListener banner_listener;
190+ banner->SetListener (&banner_listener);
191+
166192 // Make the BannerView visible.
167193 LogMessage (" Showing the banner ad." );
168194 banner->Show ();
@@ -246,14 +272,16 @@ extern "C" int common_main(int argc, const char* argv[]) {
246272
247273 // Create and test InterstitialAd.
248274 LogMessage (" Creating the InterstitialAd." );
249- LoggingInterstitialAdListener interstitial_listener;
250275 firebase::admob::InterstitialAd* interstitial =
251276 new firebase::admob::InterstitialAd ();
252- interstitial->SetListener (&interstitial_listener);
253277 interstitial->Initialize (GetWindowContext (), kInterstitialAdUnit );
254278
255279 WaitForFutureCompletion (interstitial->InitializeLastResult ());
256280
281+ // Set the listener.
282+ LoggingInterstitialAdListener interstitial_listener;
283+ interstitial->SetListener (&interstitial_listener);
284+
257285 // When the InterstitialAd is initialized, load an ad.
258286 LogMessage (" Loading an interstitial ad." );
259287 interstitial->LoadAd (request);
@@ -273,6 +301,52 @@ extern "C" int common_main(int argc, const char* argv[]) {
273301 ProcessEvents (1000 );
274302 }
275303
304+ // Start up rewarded video ads and associated mediation adapters.
305+ LogMessage (" Initializing rewarded video." );
306+ namespace rewarded_video = firebase::admob::rewarded_video;
307+ rewarded_video::Initialize ();
308+
309+ WaitForFutureCompletion (rewarded_video::InitializeLastResult ());
310+
311+ LogMessage (" Setting rewarded video listener." );
312+ LoggingRewardedVideoListener rewarded_listener;
313+ rewarded_video::SetListener (&rewarded_listener);
314+
315+ LogMessage (" Loading a rewarded video ad." );
316+ rewarded_video::LoadAd (kRewardedVideoAdUnit , request);
317+
318+ WaitForFutureCompletion (rewarded_video::LoadAdLastResult ());
319+
320+ // If an ad has loaded, show it. If the user watches all the way through, the
321+ // LoggingRewardedVideoListener will log a reward!
322+ if (rewarded_video::LoadAdLastResult ().Error () ==
323+ firebase::admob::kAdMobErrorNone ) {
324+ LogMessage (" Showing a rewarded video ad." );
325+ rewarded_video::Show (GetWindowContext ());
326+
327+ WaitForFutureCompletion (rewarded_video::ShowLastResult ());
328+
329+ // Normally Pause and Resume would be called in response to the app pausing
330+ // or losing focus. This is just a test.
331+ LogMessage (" Pausing." );
332+ rewarded_video::Pause ();
333+
334+ WaitForFutureCompletion (rewarded_video::PauseLastResult ());
335+
336+ LogMessage (" Resuming." );
337+ rewarded_video::Resume ();
338+
339+ WaitForFutureCompletion (rewarded_video::ResumeLastResult ());
340+ } else {
341+ // Rewarded Video returned an error. This might be because the
342+ // developer did not put their Rewarded Video ad unit into
343+ // kRewardedVideoAdUnit above.
344+ LogMessage (" WARNING: Is your Rewarded Video ad unit ID correct?" );
345+ LogMessage (
346+ " Ensure kRewardedVideoAdUnit is set to your own Rewarded Video ad unit "
347+ " ID in src/common_main.cc." );
348+ }
349+
276350 LogMessage (" Done!" );
277351
278352 // Wait until the user kills the app.
@@ -281,6 +355,7 @@ extern "C" int common_main(int argc, const char* argv[]) {
281355
282356 delete banner;
283357 delete interstitial;
358+ rewarded_video::Destroy ();
284359 firebase::admob::Terminate ();
285360 delete app;
286361
0 commit comments