defold / extension-admob

Defold native extension which provides access to AdMob functionality on Android and iOS
https://www.defold.com/extension-admob/
MIT License
37 stars 12 forks source link

Correct usage of api in Collection proxy #33

Closed likun123687 closed 11 months ago

likun123687 commented 11 months ago

When use this extension in collection proxy, I use the api like this:

local initialized = false
local function admob_callback(self, message_id, message)
    if message_id == admob.MSG_INITIALIZATION then
        initialized = true
        if message.event == admob.EVENT_COMPLETE then
            print("EVENT_COMPLETE: Initialization complete")
        elseif message.event == admob.EVENT_JSON_ERROR then
            print("EVENT_JSON_ERROR: Internal NE json error " .. message.error)
        end
    elseif message_id == admob.MSG_INTERSTITIAL then
        if message.event == admob.EVENT_CLOSED then
            print("ad close")
        elseif message.event == admob.EVENT_LOADED then
            print("EVENT_LOADED: Interstitial AD loaded")

        elseif message.event == admob.EVENT_FAILED_TO_LOAD then
            print(
                "EVENT_FAILED_TO_LOAD: Interstitial AD failed to load\nCode: " ..
                    message.code .. "\nError: " .. message.error)
        elseif message.event == admob.EVENT_NOT_LOADED then
            print(
                "EVENT_NOT_LOADED: can't call show_interstitial() before EVENT_LOADED\nError: " ..
                    message.error)
        end
        print("admob_callback", message_id, message.code, message.error)
    end
end

function init(self)
    if initialized == false then
        admob.set_callback(admob_callback)
        admob.set_privacy_settings(true)
        admob.request_idfa()
        admob.initialize()
        admob.set_max_ad_content_rating(admob.MAX_AD_CONTENT_RATING_G)
    end
    admob.load_interstitial(self.interstitial_ad_unit)
end

The first i load this proxy, i get the event admob.EVENT_LOADED successfully, but when i unload this proxy and load it again, I can never get the event admob.EVENT_LOADED again. Maybe the api usage like this is not right. Any help is welcome.Thanks

AGulev commented 11 months ago

When you unload proxy you remove this script component so admob_callback doesn't exist anymore. If you want to do it this way you have to set new callback again:

    admob.set_callback(admob_callback)

something like:

function init(self)
    admob.set_callback(admob_callback)
    if initialized == false then
        admob.set_privacy_settings(true)
        admob.request_idfa()
        admob.initialize()
        admob.set_max_ad_content_rating(admob.MAX_AD_CONTENT_RATING_G)
    end
    admob.load_interstitial(self.interstitial_ad_unit)
end
likun123687 commented 11 months ago

Thanks your quick reply. It solve my problem.

likun123687 commented 11 months ago

One more question.Shoud I call admob.load_interstitial function after receive INITIALIZATION COMPLETE event?

AGulev commented 11 months ago

@likun123687 you can. It's up to you to decide when you wanna load ads depends on when you gonna show it

likun123687 commented 11 months ago

@likun123687 you can. It's up to you to decide when you wanna load ads depends on when you gonna show it

What will happen if I call admob.load_interstitial before i get the INITIALIZATION COMPLETE event.

AGulev commented 11 months ago

@likun123687 I'm not sure, I think an error . You can't load ads before initialization

likun123687 commented 11 months ago

Ok, many thanks