Class: APN::Notification
- 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
-
.send_notifications(notifications) ⇒ Object
Opens a connection to the Apple APN server and attempts to batch deliver an Array of notifications.
Instance Method Summary collapse
-
#apple_hash ⇒ Object
Creates a Hash that will be the payload of an APN.
- #encode(string) ⇒ Object
-
#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(notifications) ⇒ Object
Opens a connection to the Apple APN server and attempts to batch deliver an Array of notifications. This can be run from the following Rake task:
$ rake apn:notifications:deliver
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/apn_on_rails/app/models/apn/notification.rb', line 76 def send_notifications(notifications) sent_ids = [] sent = false = '' notifications.find_each do |noty| sent_ids << noty.id << noty. end return if sent_ids.empty? begin APN::Connection.open_for_delivery do |conn, sock| sent = true conn.write() end ensure APN::Notification.update_all(['sent_at = ?', Time.now.utc], ['id in (?)', sent_ids]) if sent && sent_ids.any? end end |
Instance Method Details
#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!"}}
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/apn_on_rails/app/models/apn/notification.rb', line 33 def apple_hash 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 result['aps']['sound'] = "1.aiff" if self.sound.is_a?(TrueClass) end result.merge! payload if payload result end |
#encode(string) ⇒ Object
66 67 68 |
# File 'lib/apn_on_rails/app/models/apn/notification.rb', line 66 def encode(string) string.respond_to?(:force_encoding) ? string.force_encoding('BINARY') : string end |
#message_for_sending ⇒ Object
Creates the binary message needed to send to Apple. see developer.apple.com/IPhone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW4
60 61 62 63 64 65 |
# File 'lib/apn_on_rails/app/models/apn/notification.rb', line 60 def json = self.to_apple_json = "\0\0 #{self.device.to_hexa}\0#{encode((json.bytesize).chr)}#{encode(json)}" raise APN::Errors::ExceededMessageSizeError.new() if .bytesize.to_i > APN::Errors::ExceededMessageSizeError::MAX_BYTES 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!"}}'
54 55 56 |
# File 'lib/apn_on_rails/app/models/apn/notification.rb', line 54 def to_apple_json self.apple_hash.to_json end |