lua - Corona runtime addEventListener to be execute inside if else condition -
how run function "runtime:addeventlistener("notification", onnotification)" inside if else condition. function register gcm, default keep registering everytime open app. wanted is, call remote db save regid of device, , if exist, dont register again gcm. how do that?
local function networklistener( event ) if ( event.iserror ) 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") native.showalert("success",isreg,{"ok"}) else native.showalert("fail",isreg,{"ok"}) runtime:addeventlistener("notification", onnotification) end end end
below onnotification runtime function
-- called when notification event has been received. local function onnotification(event) print("onnotification berjalan") if event.type == "remoteregistration" -- device has been registered google cloud messaging (gcm) push notifications. -- store registration id assigned application google. googleregistrationid = event.token -- display message indicating registration successful. local message = "this app has registered google push notifications." --native.showalert("information", message, { "ok" }) printtable(message); local function submitreg( event ) if ( event.iserror ) 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 registration event log. print("### --- registration event ---") printtable(event) else -- push notification has been received. print log. print("### --- notification event ---") printtable(event.response) end end
above current code. runtime function won't execute within if else condition. how can make executing ?
it because time run check network request still in process.
try
local function networklistener( event ) if event.phase == "ended" if ( event.iserror ) 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") native.showalert("success",isreg,{"ok"}) else native.showalert("fail",isreg,{"ok"}) runtime:addeventlistener("notification", onnotification) end end end end
Comments
Post a Comment