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
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
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 = [] return [failed_messages, new_registration_messages]
end
|