coronaAds not calling Listener?

I have been through the implemenation, documentation, and github for the Corona Ads and cannot get the code to call the assigned listener. 

I can follow the code with debug statements and alerts until the coronaAds.init is called, but it does not go into the assigned listener function. I replaced the API with my own and created a listener function just before the function where coronaAds.init is called.

I can post code, but was first wondering if there were some gotchas I am missing.  

Are there requirements to call the .init in main.lua, or can I do it elsewhere as it comes needed?  And would this be true for the actual plugin require call as well (I understand it has to be called anywhere it is used, but does it have to be loaded in main.lua?  Is there a restriction on calling the .init based on conditions within the composer page??

It seems like the plugin is not getting loaded, so the calls to the .init are ignored…

Any additional insight or suggestions are appreciated.  Thanks!

Are you looking at this is in the simulator or are you building for device?

Rob

Building for device, Android.

I am able to build and run the sample project [https://github.com/coronalabs/plugins-sample-coronaAds] after swapping out the API key for a new one.

If the listener function isn’t executed, chances are that the network call associated with coronaAds.init never complete successfully. Could you please share the failing code?

Vamsee

This is all in my scorescreen.lua scene, after a round of play.  Most of the surrounding logic is to control whether or not the ads should be displayed (based on IAP or other app variation, and to only show ads every third round)

local coronaAds if (system.getInfo("targetAppStore") ~= "amazon" ) then -- comment out for MBN+ coronaAds = require( "plugin.coronaAds" ) print ("coronaAds has been required") end 

The ‘listener’ - I have not seen any debug statement (alerts or print) show up from this function

-- Event listener function local function coronaListener( event ) local alert local interstitialPlacement if (system.getInfo("targetAppStore") == "google" ) then interstitialPlacement = "interstitial-1-MBN" end alert = native.showAlert( "Ad - in Listener", "event.name = "..event.name, { "OK", "Cancel" } ) if DATA.screenshowncount ~= 0 then -- add condition for 'init'?? if ( event.phase == "found" ) then --alert = native.showAlert( "Ad - found", "should show ad next ", { "OK", "Cancel" } ) coronaAds.show( interstitialPlacement, true ) elseif ( event.phase == "failed" ) then --alert = native.showAlert( "Ad - failed", "should repeat loop next ", { "OK", "Cancel" } ) timers.t10 = timer.performWithDelay(500,showButtons) DATA.screenshowncount = 3 elseif ( event.phase == "closed" ) then --alert = native.showAlert( "Ad - closed", "should restart count ", { "OK", "Cancel" } ) timers.t10 = timer.performWithDelay(200,showButtons) DATA.screenshowncount = 1 else --alert = native.showAlert( "Ad - no phase", "condition not accounted for, event.phase = "..event.phase, { "OK", "Cancel" } ) timers.t10 = timer.performWithDelay(500,showButtons) end else --alert = native.showAlert( "Ad - count 0", "condition not tested, event.phase = "..event.phase, { "OK", "Cancel" } ) DATA.screenshowncount = 1 timers.t10 = timer.performWithDelay(500,showButtons) end alert = nil end 

 This showCoronaAds function is run, and it will stop in the condition DATA.screenshowncount == 0 (this value is set earlier) and the first attempt to init never occurs, the listener never executes.   (I have tried other ways to get the init to occur, but have not been successful)

local showCoronaAds = function () print("showCoronaAds called") local alert if DATA.adPurchased == false then if DATA.displayAds == true then print("in showCoronaAds function") -- count every third cycle if DATA.screenshowncount \>= 3 then print("call to corona.init") alert = native.showAlert( "Ad - call to init to show ad", "DATA.screenshowncount = "..DATA.screenshowncount, { "OK", "Cancel" } ) coronaAds.init( "MY API ID goes here", coronaListener ) elseif DATA.screenshowncount == 0 then print("call to corona.init in count = 0") --alert = native.showAlert( "Ad - first call to init", "DATA.screenshowncount = "..DATA.screenshowncount, { "OK", "Cancel" } ) coronaAds.init( "MY API ID goes here", coronaListener ) else DATA.screenshowncount = DATA.screenshowncount + 1 alert = native.showAlert( "Ad - no call to init", "DATA.screenshowncount = "..DATA.screenshowncount, { "OK", "Cancel" } ) print("screenshowncount = "..DATA.screenshowncount) timers.t10 = timer.performWithDelay(500,showButtons) end

The function call that gets the ball rolling (there are a couple instances of this in different conditions).  This is in the scene:show functions where phase == “will”

 timers.t9 = timer.performWithDelay(200,showCoronaAds)

I’d suggest the following changes:

  • Call coronaAds.init as early as possible in your code. This is to make sure that the associated network call completes before coronaAds.show is called. The init() function needs to be called just once.

  • Catch event.phase == ‘init’ in the listener. The ideal way to use Corona Ads would be to call show() *after* init() completes, i.e., call coronaAds.show() after an event with phase ‘init’ has been fired

Corona Ads fires the event with phase set to ‘found’ when coronaAds.show() was successful in finding an ad to display.

The issue I see with your code is that coronaAds.init is called once every three tries [no console output since you are not printing anything on event.phase == ‘init’ in your listener functions] but coronaAds.show() is never actually called. In the code coronaAds.show is called when an event with phase ‘found’ is fired but event ‘found’ is fired *after* show() finds an ad.

The right flow would be:

[lua]

function listener(event)

    if event.phase == ‘init’ then

        – This is important. coronaAds.show should be called only after we know that

        – coronaAds.init was successful.

        print( “Corona Ads init successful. Trying to load an ad”)

        coronaAds.show(PLACEMENT_ID_HERE)

    elseif event.phase == ‘found’ then

         print( “Ad displayed”)

    end

end

coronaAds.init(API_KEY, listener)

[/lua]

Here, you are waiting for init to finish successfully before calling show. For more details on events and event.phase values, please refer: https://docs.coronalabs.com/daily/plugin/coronaads/event/adsRequest/phase.html

Hope this helps.

I will try that out… I guess I misunderstood that the listener, once set up, is actively listening for the ‘init’ phase.  I think I was using the coronaAds.init call as trying to force the events to occur.

I probably am still not explaining that well, but I believe I understand what you are suggesting - so I will reload and fire again.

Thank you so much!!

I was able to show an ad!  A couple of concerns still…

The first is that after changing the structure, the ad did not show until I changed the ‘show’ to use one of the preset/default placements. This make me wonder if other iterations would have also worked had I not attempted to use the placement id I had created myself.

The other question I have is - how are following ads loaded?  Are they immediately loaded after the ‘shown’ or ‘closed’ phase? Do I need to call ‘init’ again?  I successfully displayed a banner ad and an interstitial in separate attempts, but I could not show them again when returning to the same page…  I am sure I am missing another step somewhere.

Thank you again for your help!

Hey sonyayllc,

It might take up to 24 hours for your placements to be activated - but if the app status on your dashboard is ‘Live’, you should be able to use your placement IDs for ads. If you are not seeing ads on a Live app, it is most likely a case of low- or no-fill for your device/region.

You need to call coronaAds.init() only once during the lifetime of your app; I recommend that you call coronaAds.init as soon as possible in your app.

Once init completes successfully [the listener function will catch an event with event.phase set to ‘init’ - when this happens, it means Corona Ads was successfully initialised. No need to call init again], you can use coronaAds.show(placementId) to show ads as many times as you’d like.

The following events are fired in order when coronaAds.show is called:

  • event.phase is ‘request’: denotes that Corona Ads is about to request an ad

  • event.phase is ‘found’: denotes that Corona Ads found an ad creative and is about to display it to the user

  • event.phase is ‘shown’: denotes that Corona Ads has successfully displayed an ad to user

Hope this helps. Please post the relevant code snippet if the issue persists.

Regards,

Vamsee

Are you looking at this is in the simulator or are you building for device?

Rob

Building for device, Android.

I am able to build and run the sample project [https://github.com/coronalabs/plugins-sample-coronaAds] after swapping out the API key for a new one.

If the listener function isn’t executed, chances are that the network call associated with coronaAds.init never complete successfully. Could you please share the failing code?

Vamsee

This is all in my scorescreen.lua scene, after a round of play.  Most of the surrounding logic is to control whether or not the ads should be displayed (based on IAP or other app variation, and to only show ads every third round)

local coronaAds if (system.getInfo("targetAppStore") ~= "amazon" ) then -- comment out for MBN+ coronaAds = require( "plugin.coronaAds" ) print ("coronaAds has been required") end 

The ‘listener’ - I have not seen any debug statement (alerts or print) show up from this function

-- Event listener function local function coronaListener( event ) local alert local interstitialPlacement if (system.getInfo("targetAppStore") == "google" ) then interstitialPlacement = "interstitial-1-MBN" end alert = native.showAlert( "Ad - in Listener", "event.name = "..event.name, { "OK", "Cancel" } ) if DATA.screenshowncount ~= 0 then -- add condition for 'init'?? if ( event.phase == "found" ) then --alert = native.showAlert( "Ad - found", "should show ad next ", { "OK", "Cancel" } ) coronaAds.show( interstitialPlacement, true ) elseif ( event.phase == "failed" ) then --alert = native.showAlert( "Ad - failed", "should repeat loop next ", { "OK", "Cancel" } ) timers.t10 = timer.performWithDelay(500,showButtons) DATA.screenshowncount = 3 elseif ( event.phase == "closed" ) then --alert = native.showAlert( "Ad - closed", "should restart count ", { "OK", "Cancel" } ) timers.t10 = timer.performWithDelay(200,showButtons) DATA.screenshowncount = 1 else --alert = native.showAlert( "Ad - no phase", "condition not accounted for, event.phase = "..event.phase, { "OK", "Cancel" } ) timers.t10 = timer.performWithDelay(500,showButtons) end else --alert = native.showAlert( "Ad - count 0", "condition not tested, event.phase = "..event.phase, { "OK", "Cancel" } ) DATA.screenshowncount = 1 timers.t10 = timer.performWithDelay(500,showButtons) end alert = nil end 

 This showCoronaAds function is run, and it will stop in the condition DATA.screenshowncount == 0 (this value is set earlier) and the first attempt to init never occurs, the listener never executes.   (I have tried other ways to get the init to occur, but have not been successful)

local showCoronaAds = function () print("showCoronaAds called") local alert if DATA.adPurchased == false then if DATA.displayAds == true then print("in showCoronaAds function") -- count every third cycle if DATA.screenshowncount \>= 3 then print("call to corona.init") alert = native.showAlert( "Ad - call to init to show ad", "DATA.screenshowncount = "..DATA.screenshowncount, { "OK", "Cancel" } ) coronaAds.init( "MY API ID goes here", coronaListener ) elseif DATA.screenshowncount == 0 then print("call to corona.init in count = 0") --alert = native.showAlert( "Ad - first call to init", "DATA.screenshowncount = "..DATA.screenshowncount, { "OK", "Cancel" } ) coronaAds.init( "MY API ID goes here", coronaListener ) else DATA.screenshowncount = DATA.screenshowncount + 1 alert = native.showAlert( "Ad - no call to init", "DATA.screenshowncount = "..DATA.screenshowncount, { "OK", "Cancel" } ) print("screenshowncount = "..DATA.screenshowncount) timers.t10 = timer.performWithDelay(500,showButtons) end

The function call that gets the ball rolling (there are a couple instances of this in different conditions).  This is in the scene:show functions where phase == “will”

 timers.t9 = timer.performWithDelay(200,showCoronaAds)

I’d suggest the following changes:

  • Call coronaAds.init as early as possible in your code. This is to make sure that the associated network call completes before coronaAds.show is called. The init() function needs to be called just once.

  • Catch event.phase == ‘init’ in the listener. The ideal way to use Corona Ads would be to call show() *after* init() completes, i.e., call coronaAds.show() after an event with phase ‘init’ has been fired

Corona Ads fires the event with phase set to ‘found’ when coronaAds.show() was successful in finding an ad to display.

The issue I see with your code is that coronaAds.init is called once every three tries [no console output since you are not printing anything on event.phase == ‘init’ in your listener functions] but coronaAds.show() is never actually called. In the code coronaAds.show is called when an event with phase ‘found’ is fired but event ‘found’ is fired *after* show() finds an ad.

The right flow would be:

[lua]

function listener(event)

    if event.phase == ‘init’ then

        – This is important. coronaAds.show should be called only after we know that

        – coronaAds.init was successful.

        print( “Corona Ads init successful. Trying to load an ad”)

        coronaAds.show(PLACEMENT_ID_HERE)

    elseif event.phase == ‘found’ then

         print( “Ad displayed”)

    end

end

coronaAds.init(API_KEY, listener)

[/lua]

Here, you are waiting for init to finish successfully before calling show. For more details on events and event.phase values, please refer: https://docs.coronalabs.com/daily/plugin/coronaads/event/adsRequest/phase.html

Hope this helps.

I will try that out… I guess I misunderstood that the listener, once set up, is actively listening for the ‘init’ phase.  I think I was using the coronaAds.init call as trying to force the events to occur.

I probably am still not explaining that well, but I believe I understand what you are suggesting - so I will reload and fire again.

Thank you so much!!

I was able to show an ad!  A couple of concerns still…

The first is that after changing the structure, the ad did not show until I changed the ‘show’ to use one of the preset/default placements. This make me wonder if other iterations would have also worked had I not attempted to use the placement id I had created myself.

The other question I have is - how are following ads loaded?  Are they immediately loaded after the ‘shown’ or ‘closed’ phase? Do I need to call ‘init’ again?  I successfully displayed a banner ad and an interstitial in separate attempts, but I could not show them again when returning to the same page…  I am sure I am missing another step somewhere.

Thank you again for your help!

Hey sonyayllc,

It might take up to 24 hours for your placements to be activated - but if the app status on your dashboard is ‘Live’, you should be able to use your placement IDs for ads. If you are not seeing ads on a Live app, it is most likely a case of low- or no-fill for your device/region.

You need to call coronaAds.init() only once during the lifetime of your app; I recommend that you call coronaAds.init as soon as possible in your app.

Once init completes successfully [the listener function will catch an event with event.phase set to ‘init’ - when this happens, it means Corona Ads was successfully initialised. No need to call init again], you can use coronaAds.show(placementId) to show ads as many times as you’d like.

The following events are fired in order when coronaAds.show is called:

  • event.phase is ‘request’: denotes that Corona Ads is about to request an ad

  • event.phase is ‘found’: denotes that Corona Ads found an ad creative and is about to display it to the user

  • event.phase is ‘shown’: denotes that Corona Ads has successfully displayed an ad to user

Hope this helps. Please post the relevant code snippet if the issue persists.

Regards,

Vamsee