Class: Gcm::Notification
- Extended by:
- ActionView::Helpers::TextHelper
- Includes:
- ActionView::Helpers::TextHelper
- Defined in:
- lib/gcm_rails_mongo_mapper/app/models/gcm/notification.rb
Class Method Summary collapse
-
.send_notifications(notifications = Gcm::Notification.all(:conditions => {:sent_at => nil}, :joins => :device, :readonly => false)) ⇒ Object
Opens a connection to the Google GCM server and attempts to batch deliver an Array of notifications.
Class Method Details
.send_notifications(notifications = Gcm::Notification.all(:conditions => {:sent_at => nil}, :joins => :device, :readonly => false)) ⇒ Object
Opens a connection to the Google GCM server and attempts to batch deliver an Array of notifications.
This method expects an Array of Gcm::Notifications. If no parameter is passed in then it will use the following:
Gcm::Notification.all(:conditions => {:sent_at => nil})
As each Gcm::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 gcm:notifications:deliver
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 |
# File 'lib/gcm_rails_mongo_mapper/app/models/gcm/notification.rb', line 35 def send_notifications(notifications = Gcm::Notification.all(:conditions => {:sent_at => nil}, :joins => :device, :readonly => false)) if configatron.gcm_on_rails.delivery_format and configatron.gcm_on_rails.delivery_format == 'plain_text' format = "plain_text" else format = "json" end unless notifications.nil? || notifications.empty? api_key = Gcm::Connection.open if api_key notifications.each do |notification| #puts "sending notification #{notification.id} to device #{notification.device.registration_id}" response = Gcm::Connection.send_notification(notification, api_key, format) #puts "response: #{response[:code]}; #{response.inspect}" if response[:code] == 200 if format == "json" error = "" = JSON.parse response[:message] success = ['success'] error = ['results'][0]['error'] if success == 0 else #format is plain text = response[:message] error = response[:message].split('=')[1] end case error when "MissingRegistration" ex = Gcm::Errors::MissingRegistration.new(response[:message]) #logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}") puts "#{ex.}, 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}") puts "#{ex.}, destroying gcm_device with id #{notification.device.id}" notification.device.destroy when "MismatchedSenderId" ex = Gcm::Errors::MismatchSenderId.new(response[:message]) #logger.warn(ex.message) puts ex. when "NotRegistered" ex = Gcm::Errors::NotRegistered.new(response[:message]) #logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}") puts "#{ex.}, destroying gcm_device with id #{notification.device.id}" notification.device.destroy when "MessageTooBig" ex = Gcm::Errors::MessageTooBig.new(response[:message]) #logger.warn(ex.message) puts ex. else notification.sent_at = Time.now notification.save! end elsif response[:code] == 401 raise Gcm::Errors::InvalidAuthToken.new() elsif response[:code] == 503 raise Gcm::Errors::ServiceUnavailable.new() elsif response[:code] == 500 raise Gcm::Errors::InternalServerError.new() end end end end end |