通过JSON网络请求发送Onesignal / gamethrive推送

时间:2015-05-15 05:57:18

标签: json push-notification onesignal

我正在尝试使用以下代码发送JSON请求以发送推送通知,但是我收到网络响应的错误消息“app_id not found”。我正在使用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)   

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

您在OneSignal信息中心的应用上是否同时安装了Android和Apple?使用include_player_ids时,平台(isAndroid和isIos)不是必需参数。

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

有关完整文档,请参阅OneSignal create notification POST调用。

另外,我建议您切换为使用“https://onesignal.com/api/v1/notifications”作为代码中的网址以保存重定向。