Class: Lowdown::Notification
- Inherits:
-
Object
- Object
- Lowdown::Notification
- Defined in:
- lib/lowdown/notification.rb
Overview
A Notification holds the data and metadata about a Remote Notification.
Instance Attribute Summary collapse
-
#expiration ⇒ Time?
The time until which to retry delivery of a notification.
-
#id ⇒ Object?
A object that uniquely identifies this notification and is coercable to a String.
-
#payload ⇒ Hash
The data payload for this notification.
-
#priority ⇒ Integer?
The priority at which to deliver this notification, which may be
10
or5
if power consumption should be taken into consideration. -
#token ⇒ String
A device token.
-
#topic ⇒ String?
The ‘topic’ for this notification.
Class Method Summary collapse
Instance Method Summary collapse
-
#formatted_id ⇒ String
Formats the #id in the format required by the APN service, which is in groups of 8-4-4-12.
-
#formatted_payload ⇒ Hash
Unless the payload contains an
aps
entry, the payload is assumed to be a mix of APN defined attributes and custom attributes and re-organized according to the specifications. -
#initialize(params) ⇒ Notification
constructor
A new instance of Notification.
-
#valid? ⇒ Boolean
Whether this notification holds enough data and metadata to be sent to the APN service.
Constructor Details
#initialize(params) ⇒ Notification
Returns a new instance of Notification.
61 62 63 |
# File 'lib/lowdown/notification.rb', line 61 def initialize(params) params.each { |key, value| send("#{key}=", value) } end |
Instance Attribute Details
#expiration ⇒ Time?
Returns the time until which to retry delivery of a notification. By default it is only tried once.
40 41 42 |
# File 'lib/lowdown/notification.rb', line 40 def expiration @expiration end |
#id ⇒ Object?
Returns a object that uniquely identifies this notification and is coercable to a String.
35 36 37 |
# File 'lib/lowdown/notification.rb', line 35 def id @id end |
#payload ⇒ Hash
Returns the data payload for this notification.
56 57 58 |
# File 'lib/lowdown/notification.rb', line 56 def payload @payload end |
#priority ⇒ Integer?
Returns the priority at which to deliver this notification, which may be 10
or 5
if power consumption should
be taken into consideration. Defaults to `10.
46 47 48 |
# File 'lib/lowdown/notification.rb', line 46 def priority @priority end |
#token ⇒ String
Returns a device token.
30 31 32 |
# File 'lib/lowdown/notification.rb', line 30 def token @token end |
#topic ⇒ String?
Returns the ‘topic’ for this notification.
51 52 53 |
# File 'lib/lowdown/notification.rb', line 51 def topic @topic end |
Class Method Details
.format_id(id) ⇒ Object
22 23 24 25 |
# File 'lib/lowdown/notification.rb', line 22 def self.format_id(id) padded = id.to_s.rjust(32, "0") [padded[0, 8], padded[8, 4], padded[12, 4], padded[16, 4], padded[20, 12]].join("-") end |
.generate_id ⇒ Object
16 17 18 19 20 |
# File 'lib/lowdown/notification.rb', line 16 def self.generate_id @id_mutex.synchronize do @id_counter += 1 end end |
Instance Method Details
#formatted_id ⇒ String
Formats the #id in the format required by the APN service, which is in groups of 8-4-4-12. It is padded with leading zeroes.
82 83 84 |
# File 'lib/lowdown/notification.rb', line 82 def formatted_id @formatted_id ||= self.class.format_id(id) end |
#formatted_payload ⇒ Hash
Unless the payload contains an aps
entry, the payload is assumed to be a mix of APN defined attributes and
custom attributes and re-organized according to the specifications.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/lowdown/notification.rb', line 94 def formatted_payload if @payload.key?("aps") @payload else payload = {} payload["aps"] = aps = {} @payload.each do |key, value| next if value.nil? key = key.to_s if APS_KEYS.include?(key) aps[key] = value else payload[key] = value end end payload end end |
#valid? ⇒ Boolean
Returns whether this notification holds enough data and metadata to be sent to the APN service.
68 69 70 |
# File 'lib/lowdown/notification.rb', line 68 def valid? !!(@token && @payload) end |