Created
October 31, 2016 23:47
-
-
Save sonsongithub/fd67d6573e8a134a08fde848ba14a4fd to your computer and use it in GitHub Desktop.
Script to send notification using FireBase
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
| 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.to_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