Class: APN::Notification
- Extended by:
- ActionView::Helpers::TextHelper
- Includes:
- ActionView::Helpers::TextHelper
- Defined in:
- lib/apn_on_rails/app/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.device = APN::Device.find(1)
apn.save
To deliver call the following method:
APN::Notification.send_notifications
As each APN::Notification is sent the sent_at
column will be timestamped, so as to not be sent again.
Class Method Summary collapse
Instance Method Summary collapse
-
#alert=(message) ⇒ Object
Stores the text alert message you want to send to the device.
-
#apple_hash ⇒ Object
Creates a Hash that will be the payload of an APN.
-
#message_for_sending ⇒ Object
Creates the binary message needed to send to Apple.
-
#to_apple_json ⇒ Object
Creates the JSON string required for an APN message.
Methods inherited from Base
Class Method Details
.send_notifications ⇒ Object
96 97 98 99 |
# File 'lib/apn_on_rails/app/models/apn/notification.rb', line 96 def self.send_notifications ActiveSupport::Deprecation.warn("The method APN::Notification.send_notifications is deprecated. Use APN::App.send_notifications instead.") APN::App.send_notifications end |
Instance Method Details
#alert=(message) ⇒ Object
Stores the text alert message you want to send to the device.
If the message is over 150 characters long it will get truncated to 150 characters with a ...
29 30 31 32 33 34 |
# File 'lib/apn_on_rails/app/models/apn/notification.rb', line 29 def alert=() if !.blank? && .size > 150 = truncate(, :length => 150) end write_attribute('alert', ) end |
#apple_hash ⇒ Object
Creates a Hash that will be the payload of an APN.
Example:
apn = APN::Notification.new
apn.badge = 5
apn.sound = 'my_sound.aiff'
apn.alert = 'Hello!'
apn.apple_hash # => {"aps" => {"badge" => 5, "sound" => "my_sound.aiff", "alert" => "Hello!"}}
Example 2:
apn = APN::Notification.new
apn.badge = 0
apn.sound = true
apn.custom_properties = {"typ" => 1}
apn.apple_hash # => {"aps" => {"badge" => 0, "sound" => "1.aiff"}, "typ" => "1"}
apn.alert = { 'loc-key' => 'LocalizedString.Key' }.to_json
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/apn_on_rails/app/models/apn/notification.rb', line 52 def apple_hash result = {} result['aps'] = {} if self.alert begin result['aps']['alert'] = JSON.parse(self.alert) rescue JSON::ParserError => e result['aps']['alert'] = self.alert end end result['aps']['badge'] = self.badge.to_i if self.badge if self.sound result['aps']['sound'] = self.sound if self.sound.is_a? String result['aps']['sound'] = "1.aiff" if self.sound.is_a?(TrueClass) end if self.custom_properties self.custom_properties.each do |key,value| result["#{key}"] = "#{value}" end end result end |
#message_for_sending ⇒ Object
Creates the binary message needed to send to Apple.
88 89 90 91 92 93 94 |
# File 'lib/apn_on_rails/app/models/apn/notification.rb', line 88 def json = self.to_apple_json c = "\0" = "#{c}#{c} #{self.device.to_hexa}#{c}#{json.length.chr}#{json}" raise APN::Errors::ExceededMessageSizeError.new() if .size.to_i > 256 end |
#to_apple_json ⇒ Object
Creates the JSON string required for an APN message.
Example:
apn = APN::Notification.new
apn.badge = 5
apn.sound = 'my_sound.aiff'
apn.alert = 'Hello!'
apn.to_apple_json # => '{"aps":{"badge":5,"sound":"my_sound.aiff","alert":"Hello!"}}'
83 84 85 |
# File 'lib/apn_on_rails/app/models/apn/notification.rb', line 83 def to_apple_json self.apple_hash.to_json end |