Class: APNS::Notification
- Inherits:
-
Object
- Object
- APNS::Notification
- Defined in:
- lib/apns/notification.rb
Instance Attribute Summary collapse
-
#alert ⇒ Object
Returns the value of attribute alert.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#content_available ⇒ Object
Returns the value of attribute content_available.
-
#device_token ⇒ Object
Returns the value of attribute device_token.
-
#expiration_date ⇒ Object
Returns the value of attribute expiration_date.
-
#message_identifier ⇒ Object
Returns the value of attribute message_identifier.
-
#other ⇒ Object
Returns the value of attribute other.
-
#priority ⇒ Object
Returns the value of attribute priority.
-
#sound ⇒ Object
Returns the value of attribute sound.
Instance Method Summary collapse
-
#initialize(device_token, message) ⇒ Notification
constructor
A new instance of Notification.
- #packaged_message ⇒ Object
- #packaged_notification ⇒ Object
- #packaged_token ⇒ Object
Constructor Details
#initialize(device_token, message) ⇒ Notification
Returns a new instance of Notification.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/apns/notification.rb', line 9 def initialize(device_token, ) self.device_token = device_token if .is_a?(Hash) self.alert = [:alert] self.badge = [:badge] self.sound = [:sound] self.other = [:other] self. = [:message_identifier] self.content_available = ![:content_available].nil? self.expiration_date = [:expiration_date] self.priority = if self.content_available [:priority] || 5 else [:priority] || 10 end elsif .is_a?(String) self.alert = else raise "Notification needs to have either a hash or string" end self. ||= OpenSSL::Random.random_bytes(4) end |
Instance Attribute Details
#alert ⇒ Object
Returns the value of attribute alert.
5 6 7 |
# File 'lib/apns/notification.rb', line 5 def alert @alert end |
#badge ⇒ Object
Returns the value of attribute badge.
5 6 7 |
# File 'lib/apns/notification.rb', line 5 def badge @badge end |
#content_available ⇒ Object
Returns the value of attribute content_available.
7 8 9 |
# File 'lib/apns/notification.rb', line 7 def content_available @content_available end |
#device_token ⇒ Object
Returns the value of attribute device_token.
5 6 7 |
# File 'lib/apns/notification.rb', line 5 def device_token @device_token end |
#expiration_date ⇒ Object
Returns the value of attribute expiration_date.
6 7 8 |
# File 'lib/apns/notification.rb', line 6 def expiration_date @expiration_date end |
#message_identifier ⇒ Object
Returns the value of attribute message_identifier.
6 7 8 |
# File 'lib/apns/notification.rb', line 6 def @message_identifier end |
#other ⇒ Object
Returns the value of attribute other.
5 6 7 |
# File 'lib/apns/notification.rb', line 5 def other @other end |
#priority ⇒ Object
Returns the value of attribute priority.
5 6 7 |
# File 'lib/apns/notification.rb', line 5 def priority @priority end |
#sound ⇒ Object
Returns the value of attribute sound.
5 6 7 |
# File 'lib/apns/notification.rb', line 5 def sound @sound end |
Instance Method Details
#packaged_message ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/apns/notification.rb', line 58 def aps = {'aps'=> {} } aps['aps']['alert'] = self.alert if self.alert aps['aps']['badge'] = self.badge if self.badge aps['aps']['sound'] = self.sound if self.sound aps['aps']['content-available'] = 1 if self.content_available aps.merge!(self.other) if self.other aps.to_json end |
#packaged_notification ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/apns/notification.rb', line 33 def packaged_notification pt = self.packaged_token pm = self. pi = self. pe = (self.expiration_date || 0).to_i pr = (self.priority || 10).to_i # Each item consist of # 1. unsigned char [1 byte] is the item (type) number according to Apple's docs # 2. short [big endian, 2 byte] is the size of this item # 3. item data, depending on the type fixed or variable length data = '' data << [1, pt.bytesize, pt].pack("CnA*") data << [2, pm.bytesize, pm].pack("CnA*") data << [3, pi.bytesize, pi].pack("CnA*") data << [4, 4, pe].pack("CnN") data << [5, 1, pr].pack("CnC") data end |
#packaged_token ⇒ Object
54 55 56 |
# File 'lib/apns/notification.rb', line 54 def packaged_token [device_token.gsub(/[\s|<|>]/,'')].pack('H*') end |