4

我正在尝试使用以下代码发送 JSON 请求以发送推送通知,但我收到网络响应的错误消息,提示“未找到 app_id”。我正在使用 Corona,但只要 JSON 格式正确,这并不重要。

local json = require "json"
local function networkListener( event )

    if ( event.isError ) then
        print( "Network error!" )
    else
        print ( "RESPONSE: " .. event.response )
    end
end

        headers = {}
        headers["app_id"] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        headers["Authorization"] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
        headers["Content-Type"] = "application/json"

        local params = {}

        commands_json =
             {
                ["contents"] = "English Message"
             }        

        postData = json.encode(commands_json)

        local params = {}
        params.header = headers
        params.body = postData

network.request( "https://onesignal.com/api/v1/notifications","POST",networkListener,params)   
4

2 回答 2

2

我可以解决的。

function SendJson(url, action, in_json, callback)
  local json = require ( "json" )
  local headers = {}
  headers["Content-Type"] = "application/json"
  headers["Accept-Language"] = "en-US"
 headers["Authorization"] = "Basic  12121321313123" -- put your Rest API

  local params = {}
  params.headers = headers
  params.body = json.encode( in_json )

  network.request ( url, action, callback, params )
end

local function networkListener( event )
  if ( event.isError ) then
    print( "Network error!")
  else
    print ( "RESPONSE: " .. event.response )
  end
end
local jsonToSend = {["app_id"] = "aaaaaa-222-222-33-3333333333", --put yours App ID
                    ["contents"] = {["en"] = "George challenged you to beat his score!"},
                    ["included_segments"] = "All",
                    ["isAndroid"] = true,
                    ["isIos"] = true,}
                    --["include_player_ids"] = ["(RECIPIENT_PLAYER_ID_HERE)"]}


SendJson("https://gamethrive.com/api/v1/notifications", "POST", jsonToSend, networkListener)
于 2015-05-15T06:42:15.043 回答
1

您是否在 OneSignal 仪表板上的应用程序上同时设置了 Android 和 Apple?使用 include_player_ids 时,平台(isAndroid 和 isIos)不是必需参数。

还要确保不要在生产应用程序中使用 Authorization 标头,因为有人可以轻松地反汇编您的应用程序并获取 REST API 密钥。当您使用 include_player_ids 字段时,不需要 Authorization 标头。

有关完整文档,请参阅OneSignal 创建通知POST 调用。

此外,我建议您切换到使用“ https://onesignal.com/api/v1/notifications ”作为代码中的 URL 以节省重定向。

于 2015-05-15T21:50:35.087 回答