Class: APN::App
- Defined in:
- lib/apn_on_rails/app/models/apn/app.rb
Class Method Summary collapse
-
.process_devices ⇒ Object
process_devices.
- .send_group_notifications ⇒ Object
- .send_notifications ⇒ Object
Instance Method Summary collapse
- #cert ⇒ Object
-
#process_devices ⇒ Object
Retrieves a list of APN::Device instnces from Apple using the
devices
method. - #send_group_notification(gnoty) ⇒ Object
- #send_group_notifications ⇒ Object
-
#send_notifications ⇒ Object
Opens a connection to the Apple APN server and attempts to batch deliver an Array of group notifications.
Methods inherited from Base
Class Method Details
.process_devices ⇒ Object
process_devices
108 109 110 111 112 113 |
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 108 def self.process_devices apps = APN::App.all apps.each do |app| app.process_devices end end |
Instance Method Details
#cert ⇒ Object
10 11 12 |
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 10 def cert (RAILS_ENV == 'production' ? apn_prod_cert : apn_dev_cert) end |
#process_devices ⇒ Object
Retrieves a list of APN::Device instnces from Apple using the devices
method. It then checks to see if the last_registered_at
date of each APN::Device is before the date that Apple says the device is no longer accepting notifications then the device is deleted. Otherwise it is assumed that the application has been re-installed and is available for notifications.
This can be run from the following Rake task:
$ rake apn:feedback:process
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 96 def process_devices if self.cert.nil? raise APN::Errors::MissingCertificateError.new return end APN::Feedback.devices(self.cert).each do |device| if device.last_registered_at < device.feedback_at device.destroy end end end |
#send_group_notification(gnoty) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 63 def send_group_notification(gnoty) if self.cert.nil? raise APN::Errors::MissingCertificateError.new return end unless gnoty.nil? APN::Connection.open_for_delivery({:cert => self.cert}) do |conn, sock| gnoty.devices.each do |device| conn.write(gnoty.(device)) end gnoty.sent_at = Time.now gnoty.save end end end |
#send_group_notifications ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 44 def send_group_notifications if self.cert.nil? raise APN::Errors::MissingCertificateError.new return end unless self.unsent_group_notifications.nil? || self.unsent_group_notifications.empty? APN::Connection.open_for_delivery({:cert => self.cert}) do |conn, sock| unsent_group_notifications.each do |gnoty| puts "number of devices is #{gnoty.devices.size}" gnoty.devices.each do |device| conn.write(gnoty.(device)) end gnoty.sent_at = Time.now gnoty.save end end end end |
#send_notifications ⇒ Object
Opens a connection to the Apple APN server and attempts to batch deliver an Array of group notifications.
As each APN::GroupNotification is sent the sent_at
column will be timestamped, so as to not be sent again.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 21 def send_notifications if self.cert.nil? raise APN::Errors::MissingCertificateError.new return end unless self.unsent_notifications.nil? || self.unsent_notifications.empty? APN::Connection.open_for_delivery({:cert => self.cert}) do |conn, sock| unsent_notifications.each do |noty| conn.write(noty.) noty.sent_at = Time.now noty.save end end end end |