【问题标题】:Corona runtime addEventListener to be execute inside if else conditionCorona 运行时 addEventListener 在 if else 条件下执行
【发布时间】:2014-06-27 07:28:51
【问题描述】:

如何在 if else 条件下运行此函数“Runtime:addEventListener("notification", onNotification)"。该功能是向 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 条件下执行。我怎样才能让它执行?

【问题讨论】:

  • 您能告诉我们您遇到的错误吗?添加事件侦听器只是意味着添加一个侦听器,该侦听器在“通知”事件发生时调用函数“onNotification”。我假设您只在满足条件时才需要侦听器?
  • 这里出现错误:attempt to index local 'event' (a nil value) stack traceback: [C]: ? in function 'onNotification'
  • 错误似乎出在 onNotification 函数中,能否将您的“onNotification”函数代码包含在内?
  • 我已经编辑了问题并且包含了onNotification
  • 我无法发现任何错误——可能是疏忽——但我最初的想法是“本地函数 submitReg(事件)”导致了问题。由于您正在像正常一样创建一个新函数,但是您需要记住,当您输入参数“event”并检查“event.isError”时,您实际上是从“onNotification”解析事件参数

标签: lua runtime coronasdk


【解决方案1】:

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

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    相关资源
    最近更新 更多