安卓推送通知无法工作(Corona SDK)

4
能有人为我提供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设备的方法?

这是http 400错误吗?如果是的话,那么就是对服务器的错误请求。您可以在http://www.checkupdown.com/status/E400.html上找到更多信息。 - Vinod Maurya
1个回答

2

JSON是完全没有问题的。可能存在您设置的URL问题,对于三角形通知问题,您可以通过以下链接进行查看: http://jsonlint.com/#


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接