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



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

.send_group_notificationsObject



79
80
81
82
83
84
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 79

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

.send_notificationsObject



37
38
39
40
41
42
# File 'lib/apn_on_rails/app/models/apn/app.rb', line 37

def self.send_notifications
  apps = APN::App.all 
  apps.each do |app|
    app.send_notifications
  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


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

#send_group_notificationsObject



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.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
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.message_for_sending)
        noty.sent_at = Time.now
        noty.save
      end
    end
  end
end