Skip to content

Instantly share code, notes, and snippets.

@sonsongithub
Last active May 23, 2016 16:51
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 sonsongithub/ed210c4d94a97b5177213184e1adecb1 to your computer and use it in GitHub Desktop.
Save sonsongithub/ed210c4d94a97b5177213184e1adecb1 to your computer and use it in GitHub Desktop.
Ruby script to send Firebase notification.
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse('https://fcm.googleapis.com/fcm/send')
http_proxy = ENV['HTTP_PROXY']
https_proxy = ENV['HTTPS_PROXY']
proxy_host = nil
proxy_port = nil
server_key = ""
ARGV.each{|e|
if e =~ /key=(.+?)$/
server_key = $1
end
}
if server_key == ""
puts "error"
end
if http_proxy =~ /http\:\/\/([\d\w\.]+)\:(\d+)/
proxy_host = $1
proxy_port = $2
end
https = nil
if proxy_host != nil && proxy_port != nil
https = Net::HTTP::Proxy(proxy_host, proxy_port).new(uri.host, uri.port)
else
https = Net::HTTP.new(uri.host, uri.port)
end
https.use_ssl = true
req = Net::HTTP::Post.new(uri.request_uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "key=#{server_key}"
json = Hash.new
data = Hash.new
json["to"]= "/topics/foo"
date = Time.now
info = {
:title => "title " + date.to_s,
:body => date.to_s
}
json["priority"]="high"
json["notification"]= info
# json["content_available"] = true
p json
payload = json.to_json
req.body = payload
res = https.request(req)
puts res.message
puts res.code
puts res.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment