Clarification of some questions about AdMob

Hello everyone Perhaps this has already been discussed somewhere, I searched, but did not find enough information. It is scattered somewhere in pieces. I have a vague picture on this issue. I would like someone to just clarify the situation(helped me put the puzzle together):

  1. In order to display the reward and interstitial ads, do I have to download them in advance? And if in advance, where? In the main file, or another one? I would really like to just see: which code is in main, which is in scene.
  2. To test the ad, I have to set testmod=true. But where? I would be extremely grateful if someone would show examples with the code. Thank you in advance!

Loading the ads is done with admob.load() The basics of the entire process can be seen in the sample code for admob.show() in the documentation.

To put yourself into test mode, add the testMode flag to your init parameters like this:

admob.init( adListener, { testMode = true )

The documentation is a little outdated; you no longer put your app ID in the init params. It goes in your build.settings file:

For Android, it goes here:

applicationChildElements =
        {
            [[
                <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID"
                    android:value="ca-app-pub-YOUR APP ID HERE"/>  -- See: https://goo.gl/fQ2neu
            ]],
        },

And for iOS, it is part of the plist:

GADApplicationIdentifier = "ca-app-pub-YOUR APP ID HERE",

What about rewarded ads? I understand that it is necessary to produce the “init” stage in “main”, and the “isLoaded” and "show"phase directly on the stage. As I understand it, ads should always be loaded in advance, but in my app, all the phases go on a certain scene. Should I fix this?
And does it work for both interstitial and rewards, or are there certain features?

It isn’t necessary to initialise the plugin in main.lua. In fact, I would recommend that you create a separate “ads.lua” module that contains all AdMob related code.

You can require this ads module from main and start the initialisation as soon as the app starts, but you would also be able to require the module in other scenes and handle function callbacks easier this way.

Hi. I am a beginner. I am testing admob but it doesn’t work out. What am I doing wrong?
my code is below

    local Mytext = display.newText( "start", display.contentCenterX, 100,"OpenSans- 
    Bold.ttf", 16)


     local admob = require( "plugin.admob" )

      -- AdMob listener function
     local function adListener( event )

           if ( event.phase == "init" ) then  -- Successful initialization
           -- Load an AdMob interstitial ad
            Mytext.text = "init"
            admob.load( "interstitial", { adUnitId="ca-app-pub-3940256099942544/1033173712" } )
            else
	              Mytext.text = "Dont init"
            end
       end

       -- Initialize the AdMob plugin
       admob.init( adListener, { testMode = true } )

       -- Sometime later, show the interstitial ad
       if ( admob.isLoaded( "interstitial" ) ) then
           Mytext.text = "isLoaded"
               admob.show( "interstitial" )
        else
               Mytext.text = "dont isLoaded"    
        end

admob.init() is an asynchronous function. This means that it may take your app some time before the function call completes. The app will, however, keep on executing your code.

You need to wait for admob to be initialised before you can try showing ads.

Hi. Thanks a lot. I wouldn’t have thought of it. I added a button and pressed it for a bit. The ad showed.

But there is a new problem: When I close the ad and press the button again, the ad is no longer shown

My code

    local widget = require( "widget" )
    local admob = require( "plugin.admob" )

    local Mytext = display.newText( "start", display.contentCenterX, 100,"OpenSans- 
    Bold.ttf",16)

    local function handleButtonEvent( event )
        if ( "ended" == event.phase ) then
             -- Sometime later, show the interstitial ad
             if ( admob.isLoaded( "interstitial" ) ) then
              Mytext.text = "isLoaded"
                  admob.show( "interstitial" )
             else
                  Mytext.text = "dont isLoaded"    
            end
       end
   end

local button1 = widget.newButton(
     {
        left = 100,
        top = 200,
        id = "button1",
        label = "Default",
        onEvent = handleButtonEvent
    }
)


-- AdMob listener function
  local function adListener( event )

     if ( event.phase == "init" ) then  -- Successful initialization
        -- Load an AdMob interstitial ad
        Mytext.text = "init"
        admob.load( "interstitial", { adUnitId="ca-app-pub-3940256099942544/1033173712" } )
    else
	    Mytext.text = "Dont init"
    end
 end

  -- Initialize the AdMob plugin
 admob.init( adListener, { testMode = true } )

After an ad is displayed it is gone, and you need to load a new ad. There’s an example of one way to accomplish that here:

Hi. Do I have to load the admob plugin in every scene. If not how can I use the admob variable from other scenes

You need to require the AdMob plugin any time you want to be able to call it.

If you plan on using AdMob (or anything else) in multiple scenes, you might want to consider writing a module for AdMob, and then just requiring that. It will save you from having to duplicate your listener and everything else in every scene you where want to have access to AdMob.