Skip to content

Instantly share code, notes, and snippets.

@tamphh tamphh/test_fcm.rb
Created Mar 4, 2019

Embed
What would you like to do?
Ruby script to send POST HTTPS request sample
# source: http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
# FCM ref: https://medium.com/@cdmunoz/working-easily-with-fcm-push-notifications-in-android-e1804c80f74
# How to run in terminal: ruby test_fcm.rb
require 'net/https'
require 'uri'
require 'json'
uri = URI.parse('https://fcm.googleapis.com/fcm/send')
device_token = 'FCM_device_token'
header = {
'Authorization': 'key=Legacy_server_key',
'Content-Type': 'application/json'
}
form_data = {
to: device_token,
data: {
alert: 'You received a notification!',
badge: 1,
body: 'Some body'
}
}
# Create the HTTP objects
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri, header)
request.body = form_data.to_json
# Send the request
response = http.request(request)
puts JSON.pretty_generate(JSON.parse(response.body))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.