lib/c2dm_on_rails/app/models/c2dm/notification.rb
# Opens a connection to the Google C2dm server and attempts to batch deliver
# an Array of notifications.
# 
# This method expects an Array of C2dm::Notifications. If no parameter is passed
# in then it will use the following:
#   C2dm::Notification.all(:conditions => {:sent_at => nil})
# 
# As each C2dm::Notification is sent the <tt>sent_at</tt> column will be timestamped,
# so as to not be sent again.
# 
# This can be run from the following Rake task:
#   $ rake c2dm:notifications:deliver
Opens a connection to the Google C2dm server and attempts to batch deliver
an Array of notifications.

This method expects an Array of C2dm::Notifications.

As each C2dm::Notification is sent the sent_at column will be timestamped,
so as to not be sent again.

This can be run from the following Rake task:
$ rake c2dm:notifications:deliver
Opens a connection to the Google C2dm server and attempts to batch deliver an Array of notifications . This method expects an Array of C2dm :: Notifications . As each C2dm :: Notification is sent the sent_at column will be timestamped so as to not be sent again . This can be run from the following Rake task : $ rake c2dm : notifications : deliver
def send_notifications(notifications = C2dm::Notification.all(:conditions => {:sent_at => nil}, :joins => :device, :readonly => false))
      unless notifications.nil? || notifications.empty?
        C2dm::Connection.open do |token|
          notifications.each do |noty|
            puts "sending notification #{noty.id} to device #{noty.device.registration_id}"
            response = C2dm::Connection.send_notification(noty, token)
            puts "response: #{response[:code]}; #{response.inspect}"
            if response[:code] == 200
              case response[:message]
              when "Error=QuotaExceeded"
                raise C2dm::Errors::QuotaExceeded.new(response[:message])
              when "Error=DeviceQuotaExceeded"
                ex = C2dm::Errors::DeviceQuotaExceeded.new(response[:message])
                logger.warn(ex.message)
              when "Error=InvalidRegistration"
                ex = C2dm::Errors::InvalidRegistration.new(response[:message])
                logger.warn("#{ex.message}, destroying c2dm_device with id #{noty.device.id}")
                noty.device.destroy
              when "Error=NotRegistered"
                ex = C2dm::Errors::NotRegistered.new(response[:message])
                logger.warn("#{ex.message}, destroying c2dm_device with id #{noty.device.id}")
                noty.device.destroy
              when "Error=MessageTooBig"
                ex = C2dm::Errors::MessageTooBig.new(response[:message])
                logger.warn(ex.message)
              when "Error=MissingCollapseKey"
                ex = C2dm::Errors::MissingCollapseKey.new(response[:message])
                logger.warn(ex.message)
              else
                noty.sent_at = Time.now
                noty.save!
              end
            elsif response[:code] == 503
              raise C2dm::Errors:ServiceUnavailable.new(response[:message])
            elsif response[:code] == 401
              raise C2dm::Errors::InvalidAuthToken.new(response[:message])
            else
            end
          end
        end
      end
    end
def send_notifications ( notifications = C2dm :: Notification . all ( :conditions => { :sent_at => nil } , :joins => :device , :readonly => false ) ) unless notifications . nil? || notifications . empty? C2dm :: Connection . open do | token | notifications . each do | noty | puts "sending notification #{noty.id} to device #{noty.device.registration_id}" response = C2dm :: Connection . send_notification ( noty , token ) puts "response: #{response[:code]}; #{response.inspect}" if response [ :code ] == 200 case response [ :message ] when "Error=QuotaExceeded" raise C2dm :: Errors :: QuotaExceeded . new ( response [ :message ] ) when "Error=DeviceQuotaExceeded" ex = C2dm :: Errors :: DeviceQuotaExceeded . new ( response [ :message ] ) logger . warn ( ex . message ) when "Error=InvalidRegistration" ex = C2dm :: Errors :: InvalidRegistration . new ( response [ :message ] ) logger . warn ( "#{ex.message}, destroying c2dm_device with id #{noty.device.id}" ) noty . device . destroy when "Error=NotRegistered" ex = C2dm :: Errors :: NotRegistered . new ( response [ :message ] ) logger . warn ( "#{ex.message}, destroying c2dm_device with id #{noty.device.id}" ) noty . device . destroy when "Error=MessageTooBig" ex = C2dm :: Errors :: MessageTooBig . new ( response [ :message ] ) logger . warn ( ex . message ) when "Error=MissingCollapseKey" ex = C2dm :: Errors :: MissingCollapseKey . new ( response [ :message ] ) logger . warn ( ex . message ) else noty . sent_at = Time . now noty . save! end elsif response [ :code ] == 503 raise C2dm :: Errors :ServiceUnavailable . new ( response [ :message ] ) elsif response [ :code ] == 401 raise C2dm :: Errors :: InvalidAuthToken . new ( response [ :message ] ) else end end end end end
Opens a connection to the Google C2dm server and attempts to batch deliver
an Array of notifications.
Opens a connection to the Google C2dm server and attempts to batch deliver an Array of notifications .
notifications
<not_specific>
all
<not_specific>