Class: Pling::GCM::Gateway
- Inherits:
-
Pling::Gateway
- Object
- Pling::Gateway
- Pling::GCM::Gateway
- Defined in:
- lib/pling/gcm/gateway.rb
Overview
Pling gateway to communicate with Google’s Android GCM service.
The gateway is implemented using Faraday. It defaults to Faraday’s :net_http adapter. You can customize the adapter by passing the :adapter configuration.
Instance Method Summary collapse
-
#deliver!(message, device) ⇒ Object
Sends the given message to the given device.
-
#initialize(configuration) ⇒ Gateway
constructor
Initializes a new gateway to Apple’s Push Notification service.
Methods inherited from Pling::Gateway
#deliver, discover, handled_types, handles, #handles?, #setup!, #teardown!
Constructor Details
#initialize(configuration) ⇒ Gateway
Initializes a new gateway to Apple’s Push Notification service
34 35 36 37 |
# File 'lib/pling/gcm/gateway.rb', line 34 def initialize(configuration) super require_configuration([:key]) end |
Instance Method Details
#deliver!(message, device) ⇒ Object
Sends the given message to the given device.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pling/gcm/gateway.rb', line 45 def deliver!(, device) data = { :registration_ids => [device.identifier], :data => { :body => .body, :badge => .badge, :sound => .sound, :subject => .subject }.delete_if { |_, value| value.nil? }, :collapse_key => "collapse-#{message.body.hash}" } data[:data].merge!(.payload) if configuration[:payload] && .payload response = connection.post(configuration[:push_url], data, { :Authorization => "key=#{configuration[:key]}"}) if !response.success? || response.body['failure'].to_i > 0 error_class = Pling::GCM.const_get(response.body['results'][0]['error']) rescue Pling::DeliveryFailed raise error_class.new("GCM Delivery failed: [#{response.status}] #{response.body}", , device) end end |