Class: GCM::Service

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/mercurius/gcm/service.rb

Constant Summary collapse

MAX_NUMBER_OF_RETRIES =
3
MAX_DEVICES_AT_ONCE =
999

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeService

Returns a new instance of Service.



11
12
13
14
15
16
17
# File 'lib/mercurius/gcm/service.rb', line 11

def initialize(*)
  super
  @host ||= GCM.host
  @key  ||= GCM.key
  @connection = GCM::Connection.new(@host, @key)
  @attempts = 0
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



9
10
11
# File 'lib/mercurius/gcm/service.rb', line 9

def attempts
  @attempts
end

#connectionObject (readonly)

Returns the value of attribute connection.



9
10
11
# File 'lib/mercurius/gcm/service.rb', line 9

def connection
  @connection
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/mercurius/gcm/service.rb', line 8

def host
  @host
end

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/mercurius/gcm/service.rb', line 8

def key
  @key
end

Instance Method Details

#deliver(notification, *device_tokens) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/mercurius/gcm/service.rb', line 19

def deliver(notification, *device_tokens)
  result = GCM::Result.new(notification)
  device_tokens = Array(device_tokens).flatten
  device_tokens.each_slice(MAX_DEVICES_AT_ONCE) do |tokens|
    payload = notification.to_h.merge registration_ids: tokens
    result.process_response connection.write(payload), tokens
  end
  result
end