Class: APN::Notification

Inherits:
Base
  • Object
show all
Extended by:
ActionView::Helpers::TextHelper
Includes:
AASM, ActionView::Helpers::TextHelper
Defined in:
lib/urbanairship_on_rails/models/apn/notification.rb

Overview

Represents the message you wish to send. An APN::Notification belongs to an APN::Device.

Example:

apn = APN::Notification.new
apn.badge = 5
apn.sound = 'my_sound.aiff'
apn.alert = 'Hello!'
apn.custom_properties = {:email_id => "6", :link => "http://google.com"}
apn.device = APN::Device.find(1)
apn.save

To deliver call the following method:

APN::Notification.process_pending

Constant Summary

Constants inherited from Base

Base::MAX_RETRIES, Base::OPEN_TIMEOUT, Base::READ_TIMEOUT

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#http_delete, #http_get, #http_post, #http_put, table_name

Class Method Details

.process_pendingObject



77
78
79
80
81
82
83
# File 'lib/urbanairship_on_rails/models/apn/notification.rb', line 77

def self.process_pending
  self.pending.each do |n|
    unless n.device.inactive?
      n.push
    end
  end
end

Instance Method Details

#apple_hashObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/urbanairship_on_rails/models/apn/notification.rb', line 48

def apple_hash
  result = {}
  result['aps'] = {}
  result['aps']['alert'] = self.alert if self.alert
  result['aps']['badge'] = convert(self.badge) if self.badge
  if self.sound
    result['aps']['sound'] = self.sound if self.sound
  end
  if self.custom_properties
    self.custom_properties.each do |key,value|
      result["#{key}"] = "#{value}"
    end
  end
  result['device_tokens'] = self.device.token_for_ua
  result
end

#check_responseObject



93
94
95
# File 'lib/urbanairship_on_rails/models/apn/notification.rb', line 93

def check_response
  self.last_response_code == 200 ? true : false
end

#convert(input) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/urbanairship_on_rails/models/apn/notification.rb', line 85

def convert(input)
  if input.to_i > 0 && input.to_i.to_s == input
    return input.to_i
  else
    return input
  end
end

#device_stateObject



97
98
99
# File 'lib/urbanairship_on_rails/models/apn/notification.rb', line 97

def device_state
  errors.add(:device, "must not be marked as inactive") if device.inactive?
end

#pushObject



65
66
67
68
69
70
71
# File 'lib/urbanairship_on_rails/models/apn/notification.rb', line 65

def push
  puts "process #{self.inspect}"
  result = http_post("/api/push/", apple_hash, {}, true)
  self.last_response_code = result.code
  self.save
  self.process!
end

#update_sent_atObject



73
74
75
# File 'lib/urbanairship_on_rails/models/apn/notification.rb', line 73

def update_sent_at
  self.sent_at = Time.now
end