2015年6月11日 星期四

[Corona SDK] How to add advertisement in my APP - using AdMob V2

We may want to integrate advertising bar or page in our APP.
Let's take AdMob as our example.

First, you need to visit AdMob to apply for an account.
After that, you can add the APP which you want to has Ads.
I will skip the detailed steps for that.
Basically, there have two types of Ads for AdMob.
One is called banner, and the other one is interstitial.
If your APP will be for Android and iOS platforms both, you need to apply for the Ads Unit ID separately.
That is, you will get 4 Ads Unit IDs.
Let's check the coding part.

Add plugin
First, we need to add plugin in build.settings:
settings =
{
 plugins =
    {
        ["plugin.google.play.services"] =
        {
            publisherId = "com.coronalabs"
        },        
    },
 android =
    {

        usesPermissions =
        {
            "android.permission.INTERNET",
            "android.permission.ACCESS_NETWORK_STATE",
        },
    },
}
As you can see, we also need to add access right for Android system.

Show the Ads
To show the Ads, we just need to program as follow:
local ads = require( "ads" )
local bannerAppID = "ca-app-pub-3001435268155896/xxxxxxxxxx"  --for your iOS banner
local interstitialAppID = "ca-app-pub-3001435268155896/xxxxxxxxxx"  --for your iOS interstitial
if ( system.getInfo("platformName") == "Android" ) then
    bannerAppID = "ca-app-pub-3001435268155896/xxxxxxxxxx"  --for your Android banner
    interstitialAppID = "ca-app-pub-3001435268155896/xxxxxxxxxx"  --for your Android interstitial
end

local adProvider = "admob"

local function adListener( event )
    local msg = event.response
    print( "Message from the ads library: ", msg )
 if (event.type == "banner") then
     if ( event.isError ) then
         print( "Error, no bannser ad received", msg )
     else
         print( "Got one banner AD" )
     end
 elseif(event.type == "interstitial") then
        if ( event.isError ) then
            print( "Error, no interstitial ad received", msg )
        elseif ( event.phase == "loaded" ) then
            print("interstitial ads got...")
        elseif ( event.phase == "shown" ) then
            print("interstitial has been shown and closed") 
        end
 end
end

ads.init( adProvider, bannerAppID, adListener )

ads.show( "banner", { x=0, y=0, appId=bannerAppID} )--show banner ads

ads.show( "interstitial", { x=0, y=0, appId=interstitialAppID} )--show banner ads

ads.hide() -- hide the ads
The bannerAppID and interstitialAppID are the Unit ID that  you get from AdMob websit.

We only need to call ads.init() one time.
You can pass in any type of appId.
In ads.show(), we pass in the Ads type and related appId then.

Banner Ads
ads.show( "banner", { x=0, y=0, appId=bannerAppID} ) can show the banner Ads.
The parameters {x=0, y=0} mean than we what to locate the banner in the top side.
You can decide whether to show it, as long as it will not affect the process of game.
If you want to have Ads, you'd better consider the best location for it when you are doing UI layout.

In general, we will put it in the top side or bottom side.
The reserved width should be screen-wide.
However, what about the height?
There is so called Smart Banners for AdMob.
When it is showing in phones, its height will be 32 for Landscape mode, and will be 50 for Portrait mode.
When it is showing in tablets, its height will be 90.
However, I did get banner with height in one of my phones.
Just in case, we can reserve 90 or even 100 height if possible.
It will be suitable for all cased.

Interstitial Ads 
For Interstitial Ads, it will occupy the whole screen.
It doesn't matter for what values of parameters x and y you pass in.
You should not show Interstitial Ads too frequent.
Otherwise, the user will feel that it is annoying.

The loading of Interstitial Ads may take a while.
We'd better load it in advance before showing it, as shown below:
ads.load( "interstitial", { appId=interstitialAppID } ) --load the "interstitial" ads in advance
Once it has been loaded, it will show up immediately when we call ads.show()
Before showing, we need to check if it has been loaded.
if(ads.isLoaded("interstitial"))then
 ads.show( "interstitial", { x=0, y=0, appId=interstitialAppID } )
end
Please note that once we call ads.show(), even we call ads.show( "banner", { x=0, y=0, appId=bannerAppID} ) to show the banner ads, ads.isLoaded("interstitial") will be false.
That is, the pre-loaded data will be cleared.
We need to call ads.load( "interstitial", { appId=interstitialAppID } ), ads.isLoaded("interstitial") again after we calling ads.show().
However, for Android system, once we have ever called ads.load( "interstitial", { appId=interstitialAppID } ), it will reload the data automatically.
That is, ads.isLoaded("interstitial") will be true later automatically after it has got the Interstitial Ads.
For iOS system, it will not reload it automatically. Not sure the reason.

The Ads function can only work on real devices.
We'd better add some judgement for above codes.
That is, adding system.getInfo( "environment" ) ~= "simulator" in our codes.

沒有留言:

張貼留言