- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
任何人都可以帮我编写 Google 推送通知的示例代码吗?我在 json 消息中收到错误。
这是代码:
local googleApiKey = "**********************"
local googleRegistrationId = nil
-- Called when a sent notification has succeeded or failed.
local function onSendNotification(event)
local errorMessage = nil
-- Determine if we have successfully sent the notification to Google's server.
if event.isError then
-- Failed to connect to the server.
-- This typically happens due to lack of Internet access.
errorMessage = "Failed to connect to the server."
elseif event.status == 200 then
-- A status code of 200 means that the notification was sent succcessfully.
print("Notification was sent successfully.")
elseif event.status == 400 then
-- There was an error in the sent notification's JSON data.
errorMessage = event.response
elseif event.status == 401 then
-- There was a user authentication error.
errorMessage = "Failed to authenticate the sender's Google Play account."
elseif (event.status >= 500) and (event.status <= 599) then
-- The Google Cloud Messaging server failed to process the given notification.
-- This indicates an internal error on the server side or the server is temporarily unavailable.
-- In this case, we are supposed to silently fail and try again later.
errorMessage = "Server failed to process the request. Please try again later."
end
-- Display an error message if there was a failure.
if errorMessage then
native.showAlert("Notification Error", errorMessage, { "OK" })
end
end
-- Sends the given JSON message to the Google Cloud Messaging server to be pushed to Android devices.
local function sendNotification(jsonMessage)
-- Do not continue if a Google API Key was not provided.
if not googleApiKey then
return
end
-- Print the JSON message to the log.
print("--- Sending Notification ----")
print(jsonMessage)
-- Send the push notification to this app.
local url = "**************************"
local parameters =
{
headers =
{
["Authorization"] = "key=" .. googleApiKey,
["Content-Type"] = "application/json",
},
body = jsonMessage,
}
network.request(url, "POST", onSendNotification, parameters)
end
-- Sends a push notification when the screen has been tapped.
local function onTap(event)
-- Do not continue if this app has not been registered for push notifications yet.
if not googleRegistrationId then
return
end
-- Set up a JSON message to send a push notification to this app.
-- The "registration_ids" tells Google to whom this push notification should be delivered to.
-- The "alert" field sets the message to be displayed when the notification has been received.
-- The "sound" field is optional and will play a sound file in the app's ResourceDirectory.
-- The "custom" field is optional and will be delivered by the notification event's "event.custom" property.
local jsonMessage =
[[
{
"registration_ids": ["]] .. tostring(googleRegistrationId) .. [["],
"data":
{
"alert": "Hello World!",
"sound": "notification.wav",
"custom":
{
"boolean": true,
"number": 123.456,
"string": "Custom data test.",
"array": [ true, false, 0, 1, "", "This is a test." ],
"table": { "x": 1, "y": 2 }
}
}
}
]]
sendNotification(jsonMessage)
end
Runtime:addEventListener("tap", onTap)
-- Prints all contents of a Lua table to the log.
local function printTable(table, stringPrefix)
if not stringPrefix then
stringPrefix = "### "
end
if type(table) == "table" then
for key, value in pairs(table) do
if type(value) == "table" then
print(stringPrefix .. tostring(key))
print(stringPrefix .. "{")
printTable(value, stringPrefix .. " ")
print(stringPrefix .. "}")
else
print(stringPrefix .. tostring(key) .. ": " .. tostring(value))
end
end
end
end
-- Called when a notification event has been received.
local function onNotification(event)
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" })
-- 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)
end
end
-- Set up a notification listener.
Runtime:addEventListener("notification", onNotification)
-- Print this app's launch arguments to the log.
-- This allows you to view what these arguments provide when this app is started by tapping a notification.
local launchArgs = ...
print("### --- Launch Arguments ---")
printTable(launchArgs)
每当调用此代码并出现错误 400 通知时。有没有办法使用 Corona 向 Android 设备发送自定义推送通知?
最佳答案
Json 完全没问题。可能是你设置的url有问题,对于三角通知问题,你可以通过以下链接: http://jsonlint.com/#
关于Android 推送通知不起作用(Corona sdk),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17058217/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!