Skip to content

Instantly share code, notes, and snippets.

@ugifractal
Created November 22, 2018 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugifractal/60094349ee013bb708af870eb8c97dc6 to your computer and use it in GitHub Desktop.
Save ugifractal/60094349ee013bb708af870eb8c97dc6 to your computer and use it in GitHub Desktop.
ruby Method using Faraday to send push notification on onesignal API
def self.onesignal_notify(user, message)
headers = {
"Authorization" => "BASIC #{APP_CONFIG['onesignal']['api_key']}",
"Content-Type" => "Application/json"
}
body = {
"app_id" => APP_CONFIG['onesignal']['app_id'],
"data" => {"type": "post"},
"contents" => {"en" => message},
#"include_player_ids" => [user.onesignal],
"filters" => [
{"field" => "tag", "key" => "userId", "relation" => "=", "value" => user.id.to_s}
]
}
far = Faraday.new("https://onesignal.com")
far.headers.merge!(headers)
far.params.merge!(body)
resp = far.post("/api/v1/notifications")
return resp.body
rescue Exception => e
raise e if Rails.env == 'development'
Rails.logger.error(e)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment