7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/gcm_rails_mongo_mapper/libs/connection.rb', line 7
def send_notification(notification, api_key, format)
if format == 'json'
= {"Content-Type" => "application/json",
"Authorization" => "key=#{api_key}"}
data = notification.data.merge({:collapse_key => notification.collapse_key}) unless notification.collapse_key.nil?
data = data.merge({:delay_while_idle => notification.delay_while_idle}) unless notification.delay_while_idle.nil?
data = data.merge({:time_to_live => notification.time_to_live}) unless notification.time_to_live.nil?
data = data.to_json
else = {"Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8",
"Authorization" => "key=#{api_key}"}
post_data = notification.data[:data].map{|k, v| "&data.#{k}=#{URI.escape(v)}".reduce{|k, v| k + v}}[0]
= "registration_id=#{notification.data[:registration_ids][0]}"
= "#{}&collapse_key=#{notification.collapse_key}" unless notification.collapse_key.nil?
= "#{}&delay_while_idle=1" if notification.delay_while_idle
data = "#{}#{post_data}"
end
url_string = configatron.gcm_on_rails.api_url
url = URI.parse url_string
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
resp, dat = http.post(url.path, data, )
return {:code => resp.code.to_i, :message => dat }
end
|