Skip to content

Instantly share code, notes, and snippets.

@F-3r
Created August 20, 2014 18:02
Show Gist options
  • Save F-3r/d03bfe4300156ec4f8ea to your computer and use it in GitHub Desktop.
Save F-3r/d03bfe4300156ec4f8ea to your computer and use it in GitHub Desktop.
Dirty ruby script for testing Google Cloud Messaging clients
#!/bin/env ruby
require 'net/http'
require 'uri'
require 'json'
api_key = ENV['KEY']
uri = URI('https://android.googleapis.com/gcm/send')
msg = ARGV.shift
ids = ARGV
abort "Usage: KEY=<your-api-key> gcm-test <message> (<id>)+" if api_key.nil? || ids.empty?
request = Net::HTTP::Post.new(uri)
request['Authorization'] = "Key=#{api_key}"
request['Content-Type'] = 'application/json'
request.body = { registration_ids: ids, data: { message: msg } }.to_json
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
http.request request
end
puts "headers: \n\n"
response.header.each_header {|key,value| puts "#{key} = #{value}" }
puts "\n\nbody: \n\n"
puts response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment