在if else语句中执行Corona runtime的addEventListener。
2015-1-15 3:6:56
收藏:0
阅读:105
评论:1
如何在if else条件内运行此函数“Runtime:addEventListener(“notification”,onNotification)”。该函数的作用是注册GCM,但默认情况下每次打开应用程序都会进行注册。我想要的是,调用远程数据库,保存该设备的RegID,如果存在,则不要再次向GCM注册。我该怎么做?
local function networkListener(event)
if (event.isError) then
print("网络错误!")
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("成功", isReg, {"确定"})
else
native.showAlert("失败", isReg, {"确定"})
Runtime:addEventListener("notification", onNotification)
end
end
end
##以下是onNotification运行时函数
--当接收到通知事件时调用。
local function onNotification(event)
print(“onNotification运行”)
if event.type ==“remoteRegistration”then
--此设备刚刚为Google Cloud Messaging(GCM)推送通知注册。
-通过Google分配给此应用程序的存储注册ID。
googleRegistrationId = event.token
--显示消息,指示注册成功。
local message =“此应用程序已成功注册Google推送通知。”
--native.showAlert("Information", message, { "OK" })
printTable(message);
local function submitReg(event)
if (event.isError) then
print(“网络错误!”)
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(“###---注册事件---”)
printTable(event)
else
-刚刚收到推送通知。将其打印到日志中。
print(“###---Notification Event ---”)
printTable(event.response)
end
end
以上是我的当前代码。运行时函数不会在if else条件中执行。我怎样才能使它执行?
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
这是因为在你运行检查时,网络请求仍在进行中。
试试这个:
local function networkListener( event ) if event.phase == "ended" then if ( event.isError ) then print( "网络错误!") 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("成功",isReg,{"确定"}) else native.showAlert("失败",isReg,{"确定"}) Runtime:addEventListener("notification", onNotification) end end end end