Corona运行时addEventListener在if else条件下执行

时间:2014-06-27 07:28:52

标签: lua runtime corona

如何运行此函数“Runtime:addEventListener(”notification“,onNotification)”if if else condition。该功能是向GCM注册,但默认情况下,每次打开应用程序时都会继续注册。我想要做的是,调用远程数据库,它保存设备的RegID,如果存在,不要再次注册GCM。我该怎么做?

local function networkListener( event )
    if ( event.isError ) then
            print( "Network error!")
    else
            local json = require "json"
            local t = json.decode(event.response)
            local status = t.status
            local isReg = t.isRegistered
            print(event.response)
            if (isReg == "1") then
                native.showAlert("success",isReg,{"OK"})
            else
                native.showAlert("fail",isReg,{"OK"})
                Runtime:addEventListener("notification", onNotification)
            end

    end
end

下面是onNotification运行时函数

-- Called when a notification event has been received.
local function onNotification(event)
print("onNotification berjalan")

    if event.type == "remoteRegistration" then
        -- This device has just been registered for Google Cloud Messaging     (GCM) push notifications.
        -- Store the Registration ID that was assigned to this application     by Google.
        googleRegistrationId = event.token

        -- Display a message indicating that registration was successful.
        local message = "This app has successfully registered for Google push notifications."
        --native.showAlert("Information", message, { "OK" })
        printTable(message);

        local function submitReg( event )
        if ( event.isError ) then
            print( "Network error!")
        else
            local json = require "json"
            local t = json.decode(event.response)
            print(t)        
            end
        end

        local headers = {}

        headers["Content-Type"] = "application/x-www-form-urlencoded"
        headers["Accept-Language"] = "en-US"

        local body = "key=5b41b02152251a3c19a5c3ac88c074cf11aacb19&uid="..tostring(googleRegistrationId)

        local params = {}
        params.headers = headers
        params.body = body

        print("running submit " , body)

        network.request( "http://www.domain.com/api.php?package=com.sample.app&platform=android&action=register", "POST", submitReg, params)



        -- Print the registration event to the log.
        print("### --- Registration Event ---")
        printTable(event)

    else
        -- A push notification has just been received. Print it to the log.
        print("### --- Notification Event ---")
        printTable(event.response)
    end
 end

以上是我目前的代码。运行时函数不会在if else条件下执行。我怎样才能让它执行?

1 个答案:

答案 0 :(得分:0)

这是因为当您运行检查时,网络请求仍在进行中。

试试这个

local function networkListener( event )
 if event.phase == "ended" then 

    if ( event.isError ) then
            print( "Network error!")
    else
            local json = require "json"
            local t = json.decode(event.response)
            local status = t.status
            local isReg = t.isRegistered
            print(event.response)
            if (isReg == "1") then
                native.showAlert("success",isReg,{"OK"})
            else
                native.showAlert("fail",isReg,{"OK"})
                Runtime:addEventListener("notification", onNotification)
            end

    end
end

end