Last active
July 27, 2017 11:37
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Nakama server 1.0-rc.1 use:
The main difference is you'll need to use the
nakamax
module which includeshttp_request
andjson_encode
. In the 1.0 release it's been moved into the mainnakama
module.