Skip to content

Instantly share code, notes, and snippets.

@brunofrank
Created September 9, 2017 14:47
Show Gist options
  • Save brunofrank/9a135700a5d0be11fa87ecbfdd504119 to your computer and use it in GitHub Desktop.
Save brunofrank/9a135700a5d0be11fa87ecbfdd504119 to your computer and use it in GitHub Desktop.
Class to make a push to OneSignal API
class OneSignal
APP_ID = 'ONE_SIGNAL_APP_ID'
API_KEY = 'ONE_SIGNAL_APP_KEY'
def initialize(title, body, data, players)
@title, @body, @data, @players = title, body, data, players
end
def perform
params = {
'app_id' => OneSignal::APP_ID,
'contents' => { 'en' => @body},
'headings' => { 'en' => @title},
'data' => @data,
'include_player_ids' => @players
}
uri = URI.parse('https://onesignal.com/api/v1/notifications')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json;charset=utf-8', 'Authorization' => "Basic #{OneSignal::API_KEY}")
request.body = params.as_json.to_json
response = http.request(request)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment