Skip to content

Instantly share code, notes, and snippets.

@novabyte
Last active July 27, 2017 11:37
Show Gist options
  • Save novabyte/6fdaa11d7d0d2306cefee57eebdd2650 to your computer and use it in GitHub Desktop.
Save novabyte/6fdaa11d7d0d2306cefee57eebdd2650 to your computer and use it in GitHub Desktop.
An example on how to send a push notification via One Signal with Nakama server.
local nk = require("nakama")
--[[
Send a push notification to a segment of users via One Signal.
]]--
local function send_push_notification(context, payload)
local auth = "YOUR ONE SIGNAL REST API KEY"
local url = "https://onesignal.com/api/v1/notifications"
local method = "POST"
local headers = {
["Authorization"] = ("Basic %s"):format(auth)
}
local content = nk.json_encode({
app_id = "YOUR ONE SIGNAL APP ID",
contents = {
en = "English Message"
},
included_segments = {"All"}
})
local success, code, headers, resp_body = pcall(nk.http_request, url, method, headers, content)
if (not success) then
nk.logger_error(("Failed to send push messages: %q"):format(code))
elseif (code >= 400) then
nk.logger_error(("Failed to send push message: %q %q"):format(code, resp_body))
else
nk.logger_info(("Success %q %q"):format(code, resp_body))
end
end
nk.register_rpc(send_push_notification, "one_signal")
@novabyte
Copy link
Author

For Nakama server 1.0-rc.1 use:

local nk = require("nakama")
local nx = require("nakamax")

--[[
  Send a push notification to a segment of users via One Signal.
]]--
local function send_push_notification(context, payload)
  local auth = "YOUR ONE SIGNAL REST API KEY"

  local url = "https://onesignal.com/api/v1/notifications"
  local method = "POST"
  local headers = {
    ["Authorization"] = ("Basic %s"):format(auth)
  }
  local content = nx.json_encode({
    app_id = "YOUR ONE SIGNAL APP ID",
    contents = {
      en = "English Message"
    },
    included_segments = {"All"}
  })

  local success, err = pcall(nx.http_request, url, method, headers, content)
  if (not success) then
    nk.logger_error(("Failed to send push messages: %q"):format(err))
  end
end
nk.register_rpc(send_push_notification, "one_signal")

The main difference is you'll need to use the nakamax module which includes http_request and json_encode. In the 1.0 release it's been moved into the main nakama module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment