Created
November 22, 2018 13:46
-
-
Save ugifractal/60094349ee013bb708af870eb8c97dc6 to your computer and use it in GitHub Desktop.
ruby Method using Faraday to send push notification on onesignal API
This file contains hidden or 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
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