Class: IOSPN::Notification

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
IOSPN
Includes:
ActionView::Helpers::TextHelper
Defined in:
lib/ios_push_notifications/notification.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IOSPN

configure, open, open_for_delivery, open_for_feedback

Class Method Details

.send_notifications(notifications = Notification.where(:sent_at => nil)) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ios_push_notifications/notification.rb', line 47

def self.send_notifications(notifications =Notification.where(:sent_at => nil))
  unless notifications.nil? || notifications.empty?
   open_for_delivery do |conn, sock|
      notifications.each do |noty|
        conn.write(noty.build_message_for_sending)
        noty.sent_at = Time.now
        noty.save
      end
    end
  end
end

Instance Method Details

#alert=(message) ⇒ Object



10
11
12
13
14
15
# File 'lib/ios_push_notifications/notification.rb', line 10

def alert=(message)
  if !message.blank? && message.size > 150
    message = truncate(message, :length => 150)
  end
  write_attribute(:alert, message)
end

#build_hash_for_jsonObject



33
34
35
# File 'lib/ios_push_notifications/notification.rb', line 33

def build_hash_for_json
  self.build_hash_for_push_notification.to_json
end

#build_hash_for_push_notificationObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ios_push_notifications/notification.rb', line 17

def build_hash_for_push_notification
  result = {}
  result['aps'] = {}
  result['aps']['alert'] = self.alert if self.alert
  result['aps']['badge'] = self.badge.to_i if self.badge
  if self.sound
    result['aps']['sound'] = self.sound if self.sound.is_a?(String) && self.sound
  end
  if self.custom_properties
    self.custom_properties.each do |key,value|
      result["#{key}"] = "#{value}"
    end
  end
  result
end

#build_message_for_sendingObject



37
38
39
40
41
42
43
44
45
# File 'lib/ios_push_notifications/notification.rb', line 37

def build_message_for_sending
      json = self.build_hash_for_json
message = "\0\0 #{self.device.to_hex}\0#{json.length.chr}#{json}"
if message.size.to_i > 256
  return false
else
  message
end
end