Class: Push0r::GcmService

Inherits:
Service
  • Object
show all
Defined in:
lib/push0r/GCM/GcmService.rb

Overview

GcmService is a Service implementation to push notifications to Android users using the Google Cloud Messaging Service.

Examples:

queue = Push0r::Queue.new

gcm_service = Push0r::GcmService.new("__gcm_api_token__")
queue.register_service(gcm_service)

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ GcmService

Returns a new ApnsService instance

Parameters:

  • api_key (String)

    the GCM API key obtained from the Google Developers Console



33
34
35
36
# File 'lib/push0r/GCM/GcmService.rb', line 33

def initialize(api_key)
  @api_key = api_key
  @messages = []
end

Instance Method Details

#can_send?(message) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



39
40
41
# File 'lib/push0r/GCM/GcmService.rb', line 39

def can_send?(message)
  return message.is_a?(GcmPushMessage)
end

#end_pushObject

See Also:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/push0r/GCM/GcmService.rb', line 54

def end_push
  failed_messages = []
  new_registration_messages = []

  uri = URI.parse('https://android.googleapis.com/gcm/send')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  @messages.each do |message|
    begin
      request = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json', 'Authorization' => "key=#{@api_key}"})
      request.body = message.attach({'registration_ids' => message.receiver_token}).payload.to_json
      response = http.request(request)
    rescue SocketError
      ## connection error
      failed_messages << FailedMessage.new(Push0r::GcmErrorCodes::CONNECTION_ERROR, message.receiver_token, message)
      next
    end

    if response.code.to_i == 200
      json = JSON.parse(response.body)

      if json['failure'].to_i > 0 || json['canonical_ids'].to_i > 0
        error_receivers = {}

        json['results'].each_with_index do |result, i|
          receiver_token = message.receiver_token[i]
          error = result['error']
          message_id = result['message_id']
          registration_id = result['registration_id']

          if message_id && registration_id
            new_registration_messages << NewTokenMessage.new(receiver_token, registration_id, message)
          elsif error
            error_code = Push0r::GcmErrorCodes::UNKNOWN_ERROR
            if error == 'InvalidRegistration'
              error_code = Push0r::GcmErrorCodes::INVALID_REGISTRATION
            elsif error == 'Unavailable'
              error_code = Push0r::GcmErrorCodes::UNAVAILABLE
            elsif error == 'NotRegistered'
              error_code = Push0r::GcmErrorCodes::NOT_REGISTERED
            elsif error == 'MismatchSenderId'
              error_code = Push0r::GcmErrorCodes::MISMATCH_SENDER_ID
            elsif error == 'MissingRegistration'
              error_code = Push0r::GcmErrorCodes::MISSING_REGISTRATION
            elsif error == 'MessageTooBig'
              error_code = Push0r::GcmErrorCodes::MESSAGE_TOO_BIG
            elsif error == 'InvalidDataKey'
              error_code = Push0r::GcmErrorCodes::INVALID_DATA_KEY
            elsif error == 'InvalidTtl'
              error_code = Push0r::GcmErrorCodes::INVALID_TTL
            elsif error == 'InvalidPackageName'
              error_code = Push0r::GcmErrorCodes::INVALID_PACKAGE_NAME
            end
            if error_receivers[error_code].nil? then
              error_receivers[error_code] = []
            end
            error_receivers[error_code] << receiver_token
          end
        end

        ## if there are any receivers with errors: add a hash for every distinct error code and the related receivers to the failed_messages array
        error_receivers.each do |error_code, receivers|
          failed_messages << FailedMessage.new(error_code, receivers, message)
        end
      end
    elsif response.code.to_i >= 500 && response.code.to_i <= 599
      failed_messages << FailedMessage.new(Push0r::GcmErrorCodes::INTERNAL_ERROR, message.receiver_token, message)
    else
      failed_messages << FailedMessage.new(response.code.to_i, message.receiver_token, message)
    end
  end

  @messages = [] ## reset
  return [failed_messages, new_registration_messages]
end

#init_pushObject

See Also:



49
50
51
# File 'lib/push0r/GCM/GcmService.rb', line 49

def init_push
  ## not used for gcm
end

#send(message) ⇒ Object

See Also:



44
45
46
# File 'lib/push0r/GCM/GcmService.rb', line 44

def send(message)
  @messages << message
end