Class: APN::App

Inherits:
Base
  • Object
show all
Defined in:
lib/apn_on_rails/app/models/apn/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

table_name

Class Method Details

.process_devicesObject

process_devices



121
122
123
124
125
126
127
128
129
130
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 121

def self.process_devices
  apps = APN::App.all
  apps.each do |app|
    app.process_devices
  end
  if !configatron.apn.cert.blank?
    global_cert = File.read(configatron.apn.cert)
    APN::App.process_devices_for_cert(global_cert)
  end
end

.process_devices_for_cert(the_cert) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 132

def self.process_devices_for_cert(the_cert)
  puts "in APN::App.process_devices_for_cert"
  APN::Feedback.devices(the_cert).each do |device|
    if device.last_registered_at < device.feedback_at
      puts "device #{device.id} -> #{device.last_registered_at} < #{device.feedback_at}"
      device.destroy
    else 
      puts "device #{device.id} -> #{device.last_registered_at} not < #{device.feedback_at}"
    end
  end 
end

.send_group_notificationsObject



96
97
98
99
100
101
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 96

def self.send_group_notifications
  apps = APN::App.all
  apps.each do |app|
    app.send_group_notifications
  end
end

.send_notificationsObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 29

def self.send_notifications
  apps = APN::App.all 
  apps.each do |app|
    app.send_notifications
  end
  if !configatron.apn.cert.blank?
    global_cert = File.read(configatron.apn.cert)
    send_notifications_for_cert(global_cert, nil)
  end
end

.send_notifications_for_cert(the_cert, app_id) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 40

def self.send_notifications_for_cert(the_cert, app_id)
  # unless self.unsent_notifications.nil? || self.unsent_notifications.empty?
    if (app_id == nil)
      conditions = "app_id is null and sent_at is null"
    else 
      conditions = ["app_id = ? and sent_at is null", app_id]
    end
    begin
      APN::Connection.open_for_delivery({:cert => the_cert}) do |conn, sock|
        notifications = APN::Notification.find(:all, :select => "apn_notifications.*", :conditions => conditions, :joins => " INNER JOIN apn_devices ON apn_devices.id = apn_notifications.device_id")
        notifications.each do |noty|
          conn.write(noty.message_for_sending)
          noty.sent_at = Time.now
          noty.save
        end
      end
    rescue Exception => e
      log_connection_exception(e)
    end
  # end   
end

Instance Method Details

#certObject



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_devicesObject

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


113
114
115
116
117
118
119
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 113

def process_devices
  if self.cert.nil?
    raise APN::Errors::MissingCertificateError.new
    return
  end
  APN::App.process_devices_for_cert(self.cert)
end

#send_group_notification(gnoty) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 80

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.find_each do |device|
        conn.write(gnoty.message_for_sending(device))
      end
      gnoty.sent_at = Time.now
      gnoty.save
    end
  end
end

#send_group_notificationsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 62

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|
        gnoty.devices.find_each do |device|
          conn.write(gnoty.message_for_sending(device))
        end
        gnoty.sent_at = Time.now
        gnoty.save
      end
    end
  end
end

#send_notificationsObject

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
# 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
  APN::App.send_notifications_for_cert(self.cert, self.id)
end