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

Sending Onesignal/gamethrive push by JSON network request

我正在尝试使用以下代码发送 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)   

我可以解决。

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)

您是否在 OneSignal 仪表板上的应用程序上同时设置了 Android 和 Apple?当您使用 include_player_ids.

时,平台(isAndroid 和 isIos)不是必需的参数

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

请参阅 OneSignal create notification POST 征求完整文档。

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