Class: P8push::Notification
- Inherits:
-
Object
- Object
- P8push::Notification
- Defined in:
- lib/p8push/notification.rb
Defined Under Namespace
Classes: APNSError
Constant Summary collapse
- MAXIMUM_PAYLOAD_SIZE =
2048
Instance Attribute Summary collapse
-
#alert ⇒ Object
Returns the value of attribute alert.
-
#apns_error_code ⇒ Object
writeonly
Sets the attribute apns_error_code.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#category ⇒ Object
Returns the value of attribute category.
-
#content_available ⇒ Object
Returns the value of attribute content_available.
-
#custom_data ⇒ Object
Returns the value of attribute custom_data.
-
#expiry ⇒ Object
Returns the value of attribute expiry.
-
#id ⇒ Object
Returns the value of attribute id.
-
#media_url ⇒ Object
Returns the value of attribute media_url.
-
#mutable_content ⇒ Object
Returns the value of attribute mutable_content.
-
#priority ⇒ Object
Returns the value of attribute priority.
-
#sent_at ⇒ Object
readonly
Returns the value of attribute sent_at.
-
#sound ⇒ Object
Returns the value of attribute sound.
-
#token ⇒ Object
(also: #device)
Returns the value of attribute token.
-
#topic ⇒ Object
Returns the value of attribute topic.
Instance Method Summary collapse
- #error ⇒ Object
-
#initialize(options = {}) ⇒ Notification
constructor
A new instance of Notification.
- #mark_as_sent! ⇒ Object
- #mark_as_unsent! ⇒ Object
- #message ⇒ Object
- #payload ⇒ Object
- #sent? ⇒ Boolean
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Notification
Returns a new instance of Notification.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/p8push/notification.rb', line 39 def initialize( = {}) @token = .delete(:token) || .delete(:device) @alert = .delete(:alert) @media_url = .delete(:media_url) @topic = .delete(:topic) || ENV['APN_BUNDLE_ID'] @badge = .delete(:badge) @sound = .delete(:sound) @category = .delete(:category) @expiry = .delete(:expiry) @id = .delete(:id) @priority = .delete(:priority) @content_available = .delete(:content_available) @mutable_content = .delete(:mutable_content) @custom_data = end |
Instance Attribute Details
#alert ⇒ Object
Returns the value of attribute alert.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def alert @alert end |
#apns_error_code=(value) ⇒ Object (writeonly)
Sets the attribute apns_error_code
34 35 36 |
# File 'lib/p8push/notification.rb', line 34 def apns_error_code=(value) @apns_error_code = value end |
#badge ⇒ Object
Returns the value of attribute badge.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def badge @badge end |
#category ⇒ Object
Returns the value of attribute category.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def category @category end |
#content_available ⇒ Object
Returns the value of attribute content_available.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def content_available @content_available end |
#custom_data ⇒ Object
Returns the value of attribute custom_data.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def custom_data @custom_data end |
#expiry ⇒ Object
Returns the value of attribute expiry.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def expiry @expiry end |
#id ⇒ Object
Returns the value of attribute id.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def id @id end |
#media_url ⇒ Object
Returns the value of attribute media_url.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def media_url @media_url end |
#mutable_content ⇒ Object
Returns the value of attribute mutable_content.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def mutable_content @mutable_content end |
#priority ⇒ Object
Returns the value of attribute priority.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def priority @priority end |
#sent_at ⇒ Object (readonly)
Returns the value of attribute sent_at.
33 34 35 |
# File 'lib/p8push/notification.rb', line 33 def sent_at @sent_at end |
#sound ⇒ Object
Returns the value of attribute sound.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def sound @sound end |
#token ⇒ Object Also known as: device
Returns the value of attribute token.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def token @token end |
#topic ⇒ Object
Returns the value of attribute topic.
31 32 33 |
# File 'lib/p8push/notification.rb', line 31 def topic @topic end |
Instance Method Details
#error ⇒ Object
96 97 98 |
# File 'lib/p8push/notification.rb', line 96 def error APNSError.new(@apns_error_code) if @apns_error_code && @apns_error_code.nonzero? end |
#mark_as_sent! ⇒ Object
80 81 82 |
# File 'lib/p8push/notification.rb', line 80 def mark_as_sent! @sent_at = Time.now end |
#mark_as_unsent! ⇒ Object
84 85 86 |
# File 'lib/p8push/notification.rb', line 84 def mark_as_unsent! @sent_at = nil end |
#message ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/p8push/notification.rb', line 71 def data = [device_token_item, payload_item, identifier_item, expiration_item, priority_item].compact.join [2, data.bytes.count, data].pack('cNa*') end |
#payload ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/p8push/notification.rb', line 56 def payload json = {}.merge(@custom_data || {}).inject({}) { |h, (k, v)| h[k.to_s] = v; h } json['aps'] ||= {} json['aps']['alert'] = @alert.as_json if @alert json['aps']['badge'] = @badge.to_i rescue 0 if @badge json['aps']['sound'] = @sound if @sound json['aps']['category'] = @category if @category json['aps']['content-available'] = 1 if @content_available json['aps']['mutable-content'] = 1 if @mutable_content json['media-url'] = @media_url.present? ? "#{@media_url}" : "" json end |
#sent? ⇒ Boolean
88 89 90 |
# File 'lib/p8push/notification.rb', line 88 def sent? !!@sent_at end |
#valid? ⇒ Boolean
92 93 94 |
# File 'lib/p8push/notification.rb', line 92 def valid? payload.to_json.bytesize <= MAXIMUM_PAYLOAD_SIZE end |