Class: C2DM

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/c2dm.rb

Constant Summary collapse

GCMError =
Class.new(StandardError)
API_ENDPOINT =
'https://android.googleapis.com/gcm/send'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ C2DM

Returns a new instance of C2DM.



41
42
43
# File 'lib/c2dm.rb', line 41

def initialize(api_key = nil)
  @api_key = api_key || self.class.api_key
end

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



15
16
17
# File 'lib/c2dm.rb', line 15

def api_key
  @api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/c2dm.rb', line 9

def api_key
  @api_key
end

#timeoutObject

Returns the value of attribute timeout.



9
10
11
# File 'lib/c2dm.rb', line 9

def timeout
  @timeout
end

Class Method Details

.send_notifications(notifications = []) ⇒ Object

send_notifications([

{registration_id: "aRegId1", data: {some_message: "hello", a_value: 5}, collapse_key: "optional_collapse_key"},
{registration_id: "aRegId2", data: {some_message: "weeee", a_value: 1}},
...

])



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/c2dm.rb', line 22

def send_notifications(notifications = [])
  c2dm = C2DM.new(@api_key)
  notifications.collect do |notification|
    response = nil

    begin
      response = c2dm.send_notification(notification[:registration_id], notification[:data], notification[:collapse_key])
    rescue GCMError => e
      response = e
    end
    
    {
      "registration_id" => notification[:registration_id],
      "response" => response
    }
  end
end

Instance Method Details

#send_notification(registration_id, data, collapse_key = nil) ⇒ Object

send_notification(“aRegId…”, “hello”, a_value: 5, “optional collapse_key”)

Raises:



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/c2dm.rb', line 46

def send_notification(registration_id, data, collapse_key = nil)
  raise GCMError.new("registration_id must be a String") unless registration_id.is_a?(String)
  
  payload = {
    registration_ids: [registration_id],
    data: data, 
    collapse_key: collapse_key
  }

  parse_response(send(payload))
end