Class: RubyPushNotifications::GCM::GCMConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-push-notifications/gcm/gcm_connection.rb

Overview

Encapsulates a connection to the GCM service Responsible for final connection with the service.

Author:

  • Carlos Alonso

Constant Summary collapse

GCM_URL =
'https://android.googleapis.com/gcm/send'
CONTENT_TYPE_HEADER =
'Content-Type'
JSON_CONTENT_TYPE =
'application/json'
AUTHORIZATION_HEADER =
'Authorization'

Class Method Summary collapse

Class Method Details

.post(notification, key) ⇒ GCMResponse

Issues a POST request to the GCM send endpoint to submit the given notifications.


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby-push-notifications/gcm/gcm_connection.rb', line 31

def self.post(notification, key)
  headers = {
      CONTENT_TYPE_HEADER => JSON_CONTENT_TYPE,
      AUTHORIZATION_HEADER => "key=#{key}"
  }

  url = URI.parse GCM_URL
  http = Net::HTTP.new url.host, url.port
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  response = http.post url.path, notification, headers

  GCMResponse.new response.code.to_i, response.body
end