Skip to content

Instantly share code, notes, and snippets.

@wakim
Created February 6, 2018 07:06
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 wakim/768de583623f8d697a72bf425073c92e to your computer and use it in GitHub Desktop.
Save wakim/768de583623f8d697a72bf425073c92e to your computer and use it in GitHub Desktop.
Scrip to push data messages to android devices on firebase
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
client_key = ""
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"]= client_key
date = Time.now
info = {
:title => "title " + date.to_s,
:body => date.to_s
}
data = {
:type => "offer",
:company_id => 10
}.merge(info)
json["priority"]="high"
json["content_available"] = true
json["data"] = data
#json["notification"] = info
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