Categories
Lua Tutorials

Lua code for OneSignal notification

OneSignal notification example code that are provided here include languages such as Node.js, C#, Python, PHP, Java, Ruby, Perl…, but there’s no Lua.

So here it is, the Lua version for Corona SDK (including a listener):

local json = require "json"

local function listener(e) 
	if e.isError then 
		print("Network Error: ", e.response) 
	else 
		print("Response: ", e.response) 
	end 
end 

local headers = { ["Content-Type"] = "application/json;charset=utf-8", ["Authorization"] = "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj" } 
local body = { ["app_id"] = "5eb5a37e-b458-11e3-ac11-000c2940e62c", ["included_segments"] = "All", ["data"] = {["foo"] = "bar"}, ["contents"] = {["en"] = "English Message"} } 

network.request("https://onesignal.com/api/v1/notifications", "POST", listener, { headers=headers, body=json.encode(body) })