Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug with invalid json being generated #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions lib/gcm_on_rails/app/models/gcm/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def send_notifications(notifications = Gcm::Notification.all(:conditions => {:se
response = Gcm::Connection.send_notification(notification, api_key, format)
logger.info "response = #{response.inspect}"

if response[:code] == 200
case response[:code]
when 200
if response[:message].nil?
# TODO - Making this assumption might not be right. HTTP status code 200 does not really signify success
# if Gcm servers returned nil for the message
Expand All @@ -62,39 +63,41 @@ def send_notifications(notifications = Gcm::Notification.all(:conditions => {:se


case error
when "MissingRegistration"
ex = Gcm::Errors::MissingRegistration.new(response[:message])
logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")
notification.device.destroy
when "InvalidRegistration"
ex = Gcm::Errors::InvalidRegistration.new(response[:message])
logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")
notification.device.destroy
when "MismatchedSenderId"
ex = Gcm::Errors::MismatchSenderId.new(response[:message])
logger.warn(ex.message)
when "NotRegistered"
ex = Gcm::Errors::NotRegistered.new(response[:message])
logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")
notification.device.destroy
when "MessageTooBig"
ex = Gcm::Errors::MessageTooBig.new(response[:message])
logger.warn(ex.message)
else
notification.sent_at = Time.now
notification.save!
when "MissingRegistration"
ex = Gcm::Errors::MissingRegistration.new(response[:message])
logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")
notification.device.destroy
when "InvalidRegistration"
ex = Gcm::Errors::InvalidRegistration.new(response[:message])
logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")
notification.device.destroy
when "MismatchedSenderId"
ex = Gcm::Errors::MismatchSenderId.new(response[:message])
logger.warn(ex.message)
when "NotRegistered"
ex = Gcm::Errors::NotRegistered.new(response[:message])
logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")
notification.device.destroy
when "MessageTooBig"
ex = Gcm::Errors::MessageTooBig.new(response[:message])
logger.warn(ex.message)
else
notification.sent_at = Time.now
end
elsif response[:code] == 401
notification.save!
when 400
raise Gcm::Errors::InvalidJSON.new(message_data)
when 401
raise Gcm::Errors::InvalidAuthToken.new(message_data)
elsif response[:code] == 503
raise Gcm::Errors::ServiceUnavailable.new(message_data)
elsif response[:code] == 500
when 500
raise Gcm::Errors::InternalServerError.new(message_data)
when 503
raise Gcm::Errors::ServiceUnavailable.new(message_data)
end

end
end
end
end
end
end
end
9 changes: 8 additions & 1 deletion lib/gcm_on_rails/gcm_on_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
module Gcm
module Errors

# Invalid JSON
class InvalidJSON < StandardError
def initialize(message) # :nodoc:
super("Invalid JSON: '#{message}'")
end
end

# Missing registration_id.
class MissingRegistration < StandardError
def initialize(message) # :nodoc:
Expand Down Expand Up @@ -85,4 +92,4 @@ def initialize(message)
ActiveSupport::Dependencies.autoload_paths << path
ActiveSupport::Dependencies.autoload_once_paths.delete(path)
end
end
end
12 changes: 8 additions & 4 deletions lib/gcm_on_rails/libs/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ def send_notification(notification, api_key, format)
headers = {"Content-Type" => "application/json",
"Authorization" => "key=#{api_key}"}

data = notification.data.merge({:collapse_key => notification.collapse_key}) unless notification.collapse_key.nil?
data = data.merge({:delay_while_idle => notification.delay_while_idle}) unless notification.delay_while_idle.nil?
data = data.merge({:time_to_live => notification.time_to_live}) unless notification.time_to_live.nil?
data = {
data: notification.data,
registration_ids: [notification.device.registration_id]
}
data = data.merge({collapse_key: notification.collapse_key}) unless notification.collapse_key.nil?
data = data.merge({delay_while_idle: notification.delay_while_idle}) unless notification.delay_while_idle.nil?
data = data.merge({time_to_live: notification.time_to_live}) unless notification.time_to_live.nil?
data = data.to_json
else #plain text format
headers = {"Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8",
Expand Down Expand Up @@ -40,4 +44,4 @@ def open
end
end
end
end
end